listplace.php 中的$place = $_POST['place'];
不会从 ajaxcall.php
但代码运行除了这个问题它运行正常,只有$ place没有进入我尝试了多少!请帮忙..
listplace.php文件
<?php
//connect to the mysql
$db = @mysql_connect('localhost', 'root', 'password') or die("Could not connect database");
@mysql_select_db('ajaxtest', $db) or die("Could not select database");
$place = $_POST['place'];
$sql = @mysql_query("select product_name from products_list where product_name='$place'");
$rows = array();
while($r = mysql_fetch_assoc($sql)) {
$rows[] = $r['product_name'];
}
if (count($rows)) {
echo json_encode(['option'=> $rows]);
}else {
echo json_encode(['option'=> false]);
}
?>
ajaxcall.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>
<?php
} ?>
答案 0 :(得分:1)
更改此行
data: {place: '<?= $_GET['place'] ?>'},
到
data: {place: '<?= $_GET["place"] ?>'},
答案 1 :(得分:1)
您的引号序列错误..您必须在引用单引号的字符串中使用不同类型的引号(例如:duoble)
data: {place: '<?= $_GET["place"] ?>'},
否则你在js
中有错误形成的字符串和错误