gpt4 book ai didi

php - AJAX DropDown 未填充

转载 作者:行者123 更新时间:2023-11-29 11:33:37 25 4
gpt4 key购买 nike

我正在使用 Jquery 和 PHP。这样,在选择第一个下拉列表时,第一个下拉列表的值应传递给 Mysql 查询,然后填充第二个下拉列表,但第二个下拉列表显示空白。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#city").change(function() {
var value = $(this).val();
$.ajax({
type : "GET",
url : 'abc.php',
data : {
choice : value
},
success : function(data){
$('#123').html(data);
}
})
});
});
</script>

<form action="" method="post">
<select class="form-control" id="city" action="" name="city" value="">
<option value="">--</option>
<option value="1"</option>
<option value="2"</option>
<option value="3"</option>
</select>
<br/>
</div>
<div class="form-group">
<select class="form-control" action="" name="123" id="123"">
<option value="--">--</option>
<?php
$query = "SELECT DISTINCT `Comm` FROM `Comm_New` WHERE `Market`='".$_GET['city']."' ORDER BY `Comm` ASC";
if ($result = mysqli_query($link, $query)) {
while ($Comm = mysqli_fetch_assoc($result)) {
print_r("<option value='".$Comm['Comm']."'>".$Comm['Comm']."</option>");
}
}
?>
</select><br/>
</div>

最佳答案

根据我们在评论中的对话,您正在调用与最初加载的页面相同的页面。这不一定是技术上的问题,只是没有正确实现。要加载同一页面,您需要执行以下操作:

<?php
// Make sure your database is initiated above here so this can use it.
// I am going to demonstrate a basic binding using a super basic PDO
// connection because procedural mysqli_* with bind is just annoying
$link = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
// Notice that you send "choice" as the GET key in your ajax, not "city"
if(!empty($_GET['choice'])) {
?>
<select class="form-control" action="" name="123" id="123"">
<option value="">--</option>
<?php
// prepare, bind, execute here
$query = $link->prepare("SELECT DISTINCT `Comm` FROM `Comm_New` WHERE `Market` = :0 ORDER BY `Comm` ASC");
$query->execute(array(':0'=>$_GET['choice']));
// PDO has a lot of connection settings where you can set the default
// return type so you don't need to tell it to fetch assoc here.
// Also, you would tell the the connection not to just emulate bind
// etc.. I would consider using PDO or the OOP version of mysqli
while ($Comm = $query->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='".$Comm['Comm']."'>".$Comm['Comm']."</option>";
}

?> </select>
<?php
// Stop the page from running further
die();
}
?><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#city").change(function() {
var value = $(this).val();
$.ajax({
type : "GET",
url : 'abc.php',
data : {
choice : value
},
success : function(data){
// Populate the empty container #new_drop
$('#new_drop').html(data);
}
})
});
});
</script>

<form action="" method="post">
<select class="form-control" id="city" action="" name="city" value="">
<!--
Your options are malformed. Missing close ">"
probably just copy error
-->
<option value="">--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><br/>
</div>
<!-- Add id="new_drop" to this div -->
<div class="form-group" id="new_drop">
</div>

理想情况下,您希望顶部部分位于新页面上,并且可能返回一组数据而不是直接的 html,但 ajax 非常灵活。

关于php - AJAX DropDown 未填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36991739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com