【问题标题】:Text field displays "undefined" php database?文本字段显示“未定义”的 php 数据库?
【发布时间】:2021-07-18 13:34:15
【问题描述】:

我正在尝试创建一个表单,当用户在一个文本字段中输入帐户代码时,帐户名称应在下一个文本字段中自动生成。数据库和表已经建立,但是当用户输入帐户代码时,帐户名称字段显示“未定义”。我是编码新手,我不确定如何解决这个问题。下面我附上了我的表单的样子,javascript 代码和 PHP 代码。 我的表单

脚本:

// onkeyup event will occur when the user 
        // release the key and calls the function
        // assigned to this event
        function GetDetail(str) {
            if (str.length == 0) {
                document.getElementById("account_name").value = "";
                return;
            }
            else {
  
                // Creates a new XMLHttpRequest object
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function () {
  
                    // Defines a function to be called when
                    // the readyState property changes
                    if (this.readyState == 4 && 
                            this.status == 200) {
                          
                        // Typical action to be performed
                        // when the document is ready
                        var myObj = JSON.parse(this.responseText);
  
                        // Returns the response data as a
                        // string and store this array in
                        // a variable assign the value 
                        // received to first name input field
                          
                        document.getElementById
                        ("account_name").value = myObj[1];
                          
                    }
                };
  
                // xhttp.open("GET", "filename", true);
                xmlhttp.open("GET", "cashVoucher.acc.php?user_id=" + str, true);
                  
                // Sends the request to the server
                xmlhttp.send();
            }
        }

PHP:

<?php
  
// Get the user id 
$account_code = $_REQUEST['account_code'];
  
// Database connection
$con = mysqli_connect("localhost", "root", "", "general_ledger_account");
  
if ($user_id !== "") {
      
    // Get corresponding first name and 
    // last name for that user id    
    $query = mysqli_query($con, "SELECT account_name FROM ledger_account WHERE account_code='$account_code'");
 

    $row = mysqli_fetch_array($query);
  
    // Get the first name
    $name = $row["account_name"];
  
   
}
  
// Store it in a array
$result = array("$name");
  
// Send in JSON encoded form
$myJSON = json_encode($result);
echo $myJSON;
?>

表格:

 <div class="form-group">
                        <label>Account Code</label>
                        <input type="text" class="form-control" id="account_code" name="account_code" placeholder="Debit or Credit Account Code" onkeyup="GetDetail(this.value)" value="">
                    </div>  
                    <div class="form-group">
                        <label>Account Name</label>
                        <input type="text" class="form-control" id="account_name" name="account_name"  placeholder="Debit or Credit Account Name" value="">
                    </div>  

【问题讨论】:

    标签: javascript php mysql xmlhttprequest


    【解决方案1】:

    javascript中的数组是零索引的,所以第一项的索引是0,所以代码应该是:

    document.getElementById("account_name").value = myObj[0];
    

    【讨论】:

    • 是的,我做到了。当我输入 0 时,代码不起作用。好像在我输入我的帐户代码时,帐户名称不会生成任何数据。
    • @SimiJK 将console.log(myObj); 放在上面的代码之前,然后在浏览器控制台中查看输出。这将有助于了解您的数据结构。
    • 我看不出有什么不同。还是一样。
    • 在第一个 if 之后检查 console.log(this.responseText);
    • 我看不到输出,因为表单将数据保存回我的数据库。
    【解决方案2】:

    您还需要在脚本代码和 php 代码中将 user_id 替换为 account_code,并将 myObj[1] 更改为 myObj[0] 查看更新的代码

    <?php
    
    
    // Get the user id 
    $account_code = $_REQUEST['account_code'];
      
    // Database connection
    $con = mysqli_connect("localhost", "root", "", "general_ledger_account");
      
    if ($account_code !== "") {
          
        // Get corresponding first name and 
        // last name for that user id    
        $query = mysqli_query($con, "SELECT account_name FROM ledger_account WHERE account_code='$account_code'");
     
    
        $row = mysqli_fetch_array($query);
      
        // Get the first name
        $name = $row["account_name"];
      
       
    }
      
    // Store it in a array
    $result = array("$name");
      
    // Send in JSON encoded form
    $myJSON = json_encode($result);
    echo $myJSON;
    ?>
    

    Javascript 代码

    // onkeyup event will occur when the user 
        // release the key and calls the function
        // assigned to this event
        function GetDetail(str) {
            if (str.length == 0) {
                document.getElementById("account_name").value = "";
                return;
            }
            else {
    
                // Creates a new XMLHttpRequest object
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function () {
    
                    // Defines a function to be called when
                    // the readyState property changes
                    if (this.readyState == 4 && 
                            this.status == 200) {
                          
                        // Typical action to be performed
                        // when the document is ready
                        var myObj = JSON.parse(this.responseText);
    
                        // Returns the response data as a
                        // string and store this array in
                        // a variable assign the value 
                        // received to first name input field
                          
                        document.getElementById
                        ("account_name").value = myObj[0];
                          
                    }
                };
    
                // xhttp.open("GET", "filename", true);
                xmlhttp.open("GET", "test.php?account_code=" + str, true);
                  
                // Sends the request to the server
                xmlhttp.send();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-23
      • 2011-06-18
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多