gpt4 book ai didi

javascript - 尝试使用 jquery 和 ajax 填充下拉列表

转载 作者:行者123 更新时间:2023-11-30 12:56:19 26 4
gpt4 key购买 nike

这是我的代码:-

  <script>
$(document).ready(function(){ //#This script uses jquery and ajax it is used to set the values in
$("#day").change(function(){ //# the time field whenever a day is selected.

var day=$("#day").val();
var doctor=$("#doctor").val();

$.ajax({
type:"post",
url:"time.php",
data:"day="+day+"&doctor="+doctor,
dataType : 'json',
success:function(data){
var option = '';
$.each(data.d, function(index, value) {
option += '<option>' + value.res + '</option>';
});
$('#timing').html(option);
}

});

});

});
</script>

这是 php 脚本。

  <?
$con=mysqli_connect("localhost","clinic","myclinic","myclinic");
// Check connection

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$doctor = $_POST['doctor'];

$day = $_POST['day'];

$query="SELECT * FROM schedule WHERE doctor='" .$doctor."'AND day='" .$day. "'";

$result = mysqli_query($con, $query);

$i = 0; //Initialize the variable which passes over the array key values

$row = mysqli_fetch_assoc($result); //Fetches an associative array of the row
$index = array_keys($row); // Fetches an array of keys for the row.

while($row[$index[$i]] != NULL)
{

if($row[$index[$i]] == 1) {
$res = $index[$i];
echo json_encode($res);

}
$i++;
}



?>

我想要在我的 html 页面上的选择中插入带有时间值的选项,看起来像这样:-

  <select id="timing" name="timing"></select>

我的 java 脚本代码正在向 php 脚本发送值,但代码仍然无法正常工作。正如我所见,我的 javascript 中没有任何错误。请帮帮我

最佳答案

      var postUrl = "time.php";
$.ajax({
type: "POST",
url: postUrl,
data: {day: day,doctor: doctor},
dataType: "json",
success: function (data) {
$.each(data, function (key, value) {
$('#timing').append('<option value="' + key + '">' + value + '</option>');
});
}
});

关于javascript - 尝试使用 jquery 和 ajax 填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18979369/

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