gpt4 book ai didi

php - jquery ajax 调用从 MySql 表中设置选择选项

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

我有一个 DDL (#engine),需要根据所选的 #make ddl 通过 MySql 表设置其选项值。

我有下面的脚本,它传递到一个 php 文件 - 我知道 php 文件很好,因为我可以独立输出查询结果。

但是,我无法根据返回的数组获取要更新的选项值...也许是使用 GET 的结果?这是我第一次尝试通过 AJAX 返回(通常只是用它来更新数据表等)

我的脚本有问题吗?

$(document).ready(function() {

$("select#make").change(function(){
$('#engine').find('option').remove().end(); //clear the engine ddl
var make = $(this).find("option:selected").val(); //Need the value not the text

$.ajax({
url:'get-engine.php',
type:'GET',
data:{engine:make},
dataType:'json',
cache:false,
success:function(data){


var ddl = document.getElementById('engine');

for(var c=0;c<obj.length;c++)
{
var option = document.createElement('option');
option.value = obj[c];
option.text = obj[c];
ddl.appendChild(option);
}


},
error:function(jxhr){
alert(jxhr.responseText);

}
});

});
});

还有 get-engine.php ..

$make = $_GET['engine'];

$query= ("SELECT * FROM tb_engine where make_id='$make'");
$result = mysql_query($query);
$temp = array();

while ($row = mysql_fetch_assoc($result)) {

if(empty($temp))
{
$temp=array($row['engine_type']);
}
else
{
array_push($temp,$row['engine_type']);
}

}
echo (json_encode($temp));
?>

到目前为止,它拒绝更新,不太找到原因?

提前感谢所有建议

最佳答案

你可以试试

//json is the json you have recieved in the success handler

$("#engine").empty();
$.each( json, function( index, item ) {
$("<option/>",{value:item,text:item}).appendTo("#engine");

});

DEMO

关于php - jquery ajax 调用从 MySql 表中设置选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8448110/

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