此Ajax调用在更改事件期间触发,因为它会警告$name1
变量。
但name1
变量未传递给data.php
文件
data.php
可以独立正常工作。
我该如何纠正?如何正确传递name1
变量?
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$("#name").on('change',function (e){
var name1 = this.value;
// alert("name1 = " + name1);
$.ajax ({
data:{name1: name1},
type: 'POST',
url: 'data.php',
success: function (response){
console.log(response);
$('.products-wrp').html('');
$('.products-wrp').hide();
$('.products-wrp').html(response);
$('.products-wrp').show();
}
error: function (){
alert('No data found!');
}
});
});
data.php
<?php
$name1 = $_POST['name1'];
$results = $mysqli_conn->query("SELECT product_name, product_desc, product_code, product_image, product_price FROM products_list where product_name='$name1'");
$products_list = '<ul id ="products_list" class="products-wrp">';
while($row = $results->fetch_assoc()){
$products_list .= <<<EOT
<li>
<form class="form-item">
<h4>{$row["product_name"]}</h4>
<div>
<img src="images/{$row["product_image"]}" height="62" width="62">
</div>
<div>Price :{$currency}{$row["product_price"]}<div>
</form>
</li>
EOT;
}
$products_list .= '</ul></div>';
echo $products_list;
?>
答案 0 :(得分:0)
$results = $mysqli_conn->query("SELECT product_name, product_desc, product_code, product_image, product_price FROM products_list where product_name='$name1'");
你有:
product_name='$name1'
。
不应该是:
product_name='{$name1}'