gpt4 book ai didi

javascript - PHP 使用 jquery 填充下拉框

转载 作者:行者123 更新时间:2023-11-28 20:17:20 25 4
gpt4 key购买 nike

我有一个脚本,它从 php 脚本中获取选项以填充主页上的下拉列表。

这是 JavaScript

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

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

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

这是从数据库获取数据的 php 脚本。

  <?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);

//$res = array();

echo "<select name='timing' id='timing'>";

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

//Fetches an associative array of the row
$row = mysqli_fetch_assoc($result);

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

while($row[$index[$i]] != NULL)
{
if($row[$index[$i]] == 1) {
//array_push($res, $index[$i]);
json_encode($index[$i]);

echo "<option value='" . $index[$i]."'>" . $index[$i] . "</option>";
}
$i++;
}

echo json_encode($res);

echo "</select>";

?>

它不工作。我从控制台收到错误,提示 javasrcipt 在线缺少 '}'

  $("#day").change(function(){

我似乎也找不到错误。

最佳答案

您需要在触发错误的行上方添加一个逗号:

dataType : 'json',

关于javascript - PHP 使用 jquery 填充下拉框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18974932/

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