在下面的代码中,我有一个下拉列表(id-name)
,其填充自listplace.php'. Also made another ajax call which when the dropdown item is selected it lists the specific product from
dataprod.php`
问题:当我从下拉列表中单击特定项时,它不会触发Ajax事件
我检查了dataprod.php
它是否正常工作,但即使将更改事件与我的下拉框绑定后,我也没有得到结果。请帮忙..
<select id="name">
<option selected disabled>Please select</option>
</select>
<?php if (isset($_GET['place']) && $_GET['place'] != '') { ?>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$.ajax({
type: "POST",
data: {place: '<?= $_GET["place"] ?>'},
url: 'listplace.php',
dataType: 'json',
success: function (json) {
if (json.option.length) {
var $el = $("#name");
$el.empty(); // remove old options
for (var i = 0; i < json.option.length; i++) {
$el.append($('<option>',
{
value: json.option[i],
text: json.option[i]
}));
}
}else {
alert('No data found!');
}
}
});
</script>
<script>
$(document.body).on('change',"#name",function (e) {
//doStuff
var name1 = this.value;
$.ajax ({
data: {name1: '<?= $_GET["name1"] ?>'},
url: 'dataprod.php',
success: function (response) {
console.log(response);
$('.products-wrp').html('');
$('.products-wrp').hide();
$('.products-wrp').html(response);
$('.products-wrp').show();
},
});
</script>
<?php } ?>
dataprod.php
<?php
include("config.inc.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)
您需要在ajax调用中传递选定的值。
从
更改此行var name1 = this.value;
$.ajax ({
data: {name1: '<?= $_GET["name1"] ?>'},
到
var name1 = this.value;
$.ajax ({
data: {name1: name1},
type: 'POST',
答案 1 :(得分:0)
HTML: 您可以将值存储在隐藏字段中,如:
accumulator
使用Javascript:
<input type='hidden' name='name1' id='name1' value='<?= $_GET["name1"] ?>'>