【发布时间】:2019-12-15 10:46:37
【问题描述】:
我是新手,还在学习 AJAX 调用,我不明白为什么我的 AJAX 调用只调用一个下拉列表的数据。好的,场景是我的表单中有 3 个下拉列表,分别是选择医生、显示咨询费和提供预约日期列表。我面临的问题是,当在doctor dropdown list 中选择医生时,AJAX 调用只会加载appointment date dropdown list 的数据。它没有加载咨询费。
这是我的医生代码、咨询费和预约日期下拉列表:
<div>
<label>Select Doctor</label>
<select name="doctor" id="get_doctor_name" onchange="getfee()" autocomplete="off" required>
<option hidden value="">Select Doctor</option>
</select>
</div>
<div>
<label>Consultation Fee</label>
<select name="fees" id="get_doctor_fee" autocomplete="off" readonly>
</select>
</div>
<div>
<label>Appointment Date</label>
<select name="appdate" id="get_date">
</select>
</div>
更新:这里是 AJAX 调用的脚本,函数 getfee() 和 getdate():
<script>
//function for fee details
function getfee()
{
$("#loaderIcon").show();
jQuery.ajax(
{
url: "getfee.php",
data: {doctor : $("#get_doctor_name").val()},
type: "POST",
success: function(data)
{
$("#get_doctor_fee").html(data);
$("#loaderIcon").hide();
},
error: function() {}
});
}
//function for appointment date details
function getdate() {
$("#loaderIcon").show();
jQuery.ajax({
url: "getslot-date.php",
data: {doctor : $("#get_doctor_name").val()},
type: "POST",
success: function(data) {
$("#get_date").html(data);
$("#loaderIcon").hide();
},
error: function() {}
});
}
</script>
以下是 AJAX 调用脚本中getfee.php 中的代码:
<?php
include('incl/connection.php');
if(!empty($_POST['doctor']))
{
$doctor =$_POST['doctor'];
$sql = "SELECT D_FEES FROM tbldoctor
WHERE D_ID=:doctor";
$query = $dbh->prepare($sql);
$query->bindParam(':doctor',$doctor,PDO::PARAM_STR);
$query->execute();
if (!$query->execute()) {
print_r($query->errorInfo());
}
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $results)
{
echo '<option readonly value="'.htmlentities($results->D_FEES).'">'. htmlentities($results->D_FEES).'</option>';
}
}
else
{
echo '<option value=""> No Doctor in this specilization</option>';
echo "<script>$('#submit').prop('disabled',true);</script>";
}
}
?>
以下是 AJAX 调用脚本中getslot-date.php 中的代码:
<?php
include('incl/connection.php');
if(!empty($_POST['doctor']))
{
$doctor =$_POST['doctor'];
$sql = "SELECT T_ID, T_DATE, T_TIME FROM tbltimeslot
WHERE D_ID=:doctor AND A_ID IS NULL";
$query = $dbh->prepare($sql);
$query->bindParam(':doctor',$doctor,PDO::PARAM_STR);
$query->execute();
if (!$query->execute()) {
print_r($query->errorInfo());}
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $results)
{
echo '<option value="'.htmlentities($results->T_ID).'">'.htmlentities($results->T_DATE).' @ '.htmlentities($results->T_TIME).'</option>';
}
}
else
{
echo '<option value=""> No available date</option>';
echo "<script>$('#submit').prop('disabled',true);</script>";
}
}
?>
下面是tbldoctor和tbltimeslot的表格:
CREATE TABLE `tbldoctor` (
`D_ID` int(11) NOT NULL,
`D_SPECILIZATION` varchar(255) NOT NULL,
`D_NAME` varchar(255) NOT NULL,
`D_FEES` varchar(255) DEFAULT NULL
)
CREATE TABLE `tbltimeslot` (
`T_ID` int(11) NOT NULL,
`T_DATE` date NOT NULL,
`T_TIME` time NOT NULL,
`D_ID` int(11) NOT NULL,
`A_ID` int(11) DEFAULT NULL
)
【问题讨论】:
-
看起来只是一个错字。您有两个名为
getfee的函数,因此一个会覆盖另一个。要么把你的两个操作放在一个函数中,要么给你的函数不同的名字。 -
只需重命名这两个函数之一,这样您就可以知道您正在调用哪一个。
-
喜欢这个@MahmoudMostafa @David?
<select class="form-control" name="doctor" id="get_doctor_name" onchange="getfee()" onchange="getdate()" autocomplete="off" required>。我试图运行代码,但现在,日期的下拉列表没有显示任何数据 -
fyi,当点击你的
else分支时......我不确定<script>是否正在<select>内执行 -
不要在 HTML 中使用两个
onchange。 cmets 想说的是:在您的 javascript 中有两个function getfee() {,将第二个重命名为function getdate() {。还阅读了datashould be passed(基本上是 Mohammad Taghizad Badrian 的回答)
标签: javascript php jquery ajax