Php AJAX SQL JSON不工作
我的AJAX似乎不起作用。它应该将患者详细信息加载到dropwdown中 这是我的ajax代码:Php AJAX SQL JSON不工作,php,jquery,json,ajax,Php,Jquery,Json,Ajax,我的AJAX似乎不起作用。它应该将患者详细信息加载到dropwdown中 这是我的ajax代码: function PopulatePatient() { $("#PatientDropDown").empty(); $("#PatientDropDown").append("<option>Loading.....</option>"); $.ajax({ type:"POST",
function PopulatePatient()
{
$("#PatientDropDown").empty();
$("#PatientDropDown").append("<option>Loading.....</option>");
$.ajax({
type:"POST",
url:"../listener/populatePatientName.php",
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(data){
$("#PatientDropDown").empty();
$("#PatientDropDown").append("<option value=''>-Select Patient-</option>");
$.each(data,function(i,item)
{
$("#PatientDropDown").append('<option value="'+ data[i].patientUserId +'">'+ data[i].patientFirstName +'</option>');
});
},
complete: function()
{
}
});
}
这是我的PHP代码populatePatientName.PHP:
<?php
include '../core/init.php';
$sql = mysql_query("SELECT patientUserId,patientFirstName FROM patientdetails");
if(mysql_num_rows($sql))
{
$data=array();
while($row=mysql_fetch_array($sql))
{
$data[]=array
(
'patientUserId' => $row['patientUserId']
'patientFirstName' => $row['patientFirstName']
);
}
header('Content-type: application/json');
echo json_encode($data);
}
?>
这是我的HTML代码:
<select class="form-control" id="PatientDropDown">
</select>
这段代码在我的朋友PC上运行。我的数据库和查询运行良好。表中包含数据
我的代码到此为止:
$PatientDropDown.appendLoading
它不进入ajax。有什么解决方案吗?在$之前尝试这一行。ajax中的每个函数都会成功
data= JSON.parse(data);
使用GET而不是POST进入ajax类型。您的脚本非常好,但没有任何事件会触发PopulatePatient函数。运行函数以获取数据。使用console.logdata检查您的php是否正在发送数据。尝试使用web浏览器转到populatePatientName.php,它应该返回JSON数据我找到了解决方案,我忘记了逗号
<?php
include '../core/init.php';
$sql = mysql_query("SELECT patientUserId,patientFirstName FROM patientdetails");
if(mysql_num_rows($sql))
{
$data=array();
while($row=mysql_fetch_array($sql))
{
$data[]=array
(
'patientUserId' => $row['patientUserId'],
'patientFirstName' => $row['patientFirstName']
);
}
header('Content-type: application/json');
echo json_encode($data);
}
两个问题:
-它包含在jquery的head标记中,像这样
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
-要填充,请尝试使用以下按钮:
<input type="button" onClick="populatePatient()" />
您是否在浏览器控制台上收到任何错误?是的:[{patientUserId:10001,patientFirstName:Mohamad Shahiran},{patientUserId:10002,patientFirstName:Mohamad Shahiran},{patientUserId:10003,patientFirstName:Mohamad Shakir}]
<input type="button" onClick="populatePatient()" />