错误的回应ajax

时间:2016-02-23 04:08:49

标签: php jquery ajax

我尝试从我的数据库填充文本框。我正在使用onChange事件。我可以看到代码正在运行,但我得到了错误的响应。

我使用以下代码:

的index.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM cus";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<select class='form-control select2' name='cus_id' onChange='getCus(this.value)' style='width: 100%;'>";
              echo "<option selected disabled hidden value=''></option>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
                      echo "<option value='" . $row["cus_id"]. "'>" . $row["cus_id"].  " | " . $row["cus_name"]. "</option>";
     }                   
echo "</select>";
} else {
     echo "0 results";
}

$conn->close();

?>  
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function getCus() {

    // getting the selected id in combo
    var selectedItem = jQuery('#cus_id option:selected').val();

    // Do an Ajax request to retrieve the product price
jQuery.ajax({
    url: 'get5.php',
    method: 'POST',
    data: {'cus_id': selectedItem}, 
    success: function(response){
        // and put the price in text field
        jQuery('#cus_id').val(response);
    },
    error: function (request, status, error) {
        alert(request.responseText);
    },
}); 
}
</script>

get5.php

<?php

// Turn off all error reporting
error_reporting(0);

?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname) ;
// Check connection
if ($conn->connect_error) 
    {
    die('Connection failed: ' . $conn->connect_error) ;
    } 
else 
    {
    $cus_name = isset($_POST['cus_name'])?$_POST['cus_name']:'';
    $cus_ad = isset($_POST['cus_ad'])?$_POST['cus_ad']:'';
    $cus_pos = isset($_POST['cus_pos'])?$_POST['cus_pos']:'';
    $cus_pla = isset($_POST['cus_pla'])?$_POST['cus_pla']:'';
    $cus_cou = isset($_POST['cus_cou'])?$_POST['cus_cou']:'';

    $query = 'SELECT * FROM cus WHERE cus_id=' . $cus_id . ' ';

    $res = mysqli_query($conn, $query) ;
if (mysqli_num_rows($res) > 0) 
{
    $result = mysqli_fetch_assoc($res) ;
    echo $result['cus_name'];
    echo $result['cus_ad'];
    echo $result['cus_pos'];
    echo $result['cus_pla'];
    echo $result['cus_cou'];

}else{
    $result = mysqli_fetch_assoc($res) ;
    echo "0 res";

}

    }
?>

我无法看到我的代码有什么问题。当我运行这个时,我收到响应“0 res”。任何人都可以看到有什么问题吗?

2 个答案:

答案 0 :(得分:0)

你需要这行代码

<?php    
// Turn off all error reporting
error_reporting(0);

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname) ;
// Check connection
if ($conn->connect_error) {
    die('Connection failed: ' . $conn->connect_error) ;
}else {
    $cus_id = isset($_POST['cus_id'])?$_POST['cus_id']:'';

    $query = 'SELECT * FROM cus WHERE cus_id="' . mysqli_real_escape_string($conn, $cus_id) . '"';    
    $res = mysqli_query($conn, $query) ;
  if (mysqli_num_rows($res) > 0) {
     $result = mysqli_fetch_assoc($res) ;
     echo $result['cus_name'];
     echo $result['cus_ad'];
     echo $result['cus_pos'];
     echo $result['cus_pla'];
     echo $result['cus_cou'];    
  }else{
     $result = mysqli_fetch_assoc($res) ;
     echo "0 res";    
  }    
} ?>

您应该始终转义字符串中的特殊字符,以便在SQL语句中使用。

答案 1 :(得分:0)

请检查您的cus_id数据类型,我猜您没有将其设置为整数或其他数字类型。 如果我错了,你应该在数据库中检查你的行,它在cus_id中必须是空的。