PHP 用 jquery 填充下拉框
PHP populate drop box with jquery
我有一个脚本,它从脚本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方面,你期望JSON,但在PHP方面,你生成HTML。
使用 JSON 在服务器和浏览器之间发送数据。确保实际生成有效的 JSON 且仅生成 JSON。
此行不执行任何操作(函数返回值,但不修改它)
json_encode($index[$i]);
这一行没有意义 - 变量 $res 未初始化;
echo json_encode($res);
相关文章:
- 用我的json数据填充JQuery DataTable
- 解析和填充 jQuery 插件的选项数组
- 试图用php文件填充jquery
- 用 angularjs 填充 jquery 移动表
- 从替代字段填充 jquery UI 日期选择器日期字段
- 使用 ajax 从 mysql 填充 jQuery 数据表
- 尝试从 JSON 文件填充 JQuery 中的列表.如何调试
- 用服务器数组填充 jquery 数组
- 如何使用 Restangular 填充 jQuery UI Select2
- 如何从 Ajax 响应中重新填充 jquery UI 日期选择器
- 从 XML 填充 jquery 表单
- 使用每个选择的值填充 jquery 数组
- 如何从 pouchdb 数据填充 jquery 移动列表视图
- 用数据预填充 jQuery 自动完成
- 用如何刷新列表填充 jquery 页面
- 用javascript变量填充jquery模态表单字段值
- 重新填充Jquery多选框
- 用多个列表填充jquery select下拉表单
- 动态填充jquery UI类别自动完成
- 从数组中随机选择一个项目并填充-jQuery