下拉值的AJAX调用不显示数据

时间:2019-12-14 11:51:53

标签: php jquery ajax

我对使用AJAX调用功能不熟悉,我试图弄清楚为什么我的下拉列表不显示数据库中的数据。好的,情况是我的表单中有2个下拉列表。第一个是选择“专业化”,第二个是选择医生。当我选择一个专业时,它将加载该特定专业下的医生列表。 AJAX调用的功能是在医生下拉列表中呼叫医生的姓名。我无法在下拉列表中显示医生列表。

以下是我的专业化下拉列表的代码:

<div>
  <label>Doctor Specilization</label>
  <select name="specilization" id="specilization" onBlur="getspec()" autocomplete="off" required>
    <?php 
                  $sqlspecili = "SELECT S_NAME FROM tblspecilization";
                      $speciliquery = $dbh -> prepare($sqlspecili);
                      $speciliquery->execute();
                      $speciliresults=$speciliquery->fetchAll(PDO::FETCH_OBJ);
                      $specilicnt=1; ?>

    <option selected hidden value="">Select specialization</option>
    <?php if($speciliquery->rowCount() > 0) {
                        foreach($speciliresults as $speciliresults) { ?>
    <option value="<?php echo htmlentities($speciliresults->S_NAME);?>">
      <?php echo htmlentities($speciliresults->S_NAME);?> </option>
    <?php $specilicnt=$specilicnt+1;
                          }
                        } ?>
  </select>
</div>

这是我的医生下拉列表的代码:

<div>
  <label>Select Doctor</label>
  <select name="doctor" id="get_doctor_name" autocomplete="off" required>
  </select>
</div>

这是我的AJAX调用的脚本:

    <script>
      function getspec() {
        $("#loaderIcon").show();
        jQuery.ajax({
          url: "get_doctor.php",
          data: 'specilization=' + $("#specilization").val(),
          type: "POST",
          success: function(data) {
            $("#get_doctor_name").html(data);
            $("#loaderIcon").hide();
          },
          error: function() {}
        });
    </script>

这是在AJAX调用中调用的get_doctor.php

<?php
include('incl/conn.php');
if(!empty($_POST['specilization'])) 
{
    $specilization =:$_POST['specilization'];

    $sql = "SELECT D_ID, D_NAME FROM tbldoctor 
            WHERE D_SPECILIZATION=:specilization";

    $query = $dbh->prepare($sql);
    $query->bindParam(':specilization',$specilization,PDO::PARAM_STR);
    $query->execute();
    $results=$query->fetchAll(PDO::FETCH_OBJ);
    $cnt=1;
    <option selected="selected">Select Doctor</option>
    if($query->rowCount() > 0) 
    {
        foreach($results as $results) 
            { ?>
            <option value="<?php echo htmlentities($results->D_ID);?>" >
                <?php echo htmlentities($results->D_NAME);?>
            </option> <?php
            }
    }
    else
    { ?>
        <option value=""> No Doctor in this specilization</option>
        <?php echo "<script>$('#submit').prop('disabled',true);</script>"; 
    }
}?>

这是tbldoctor的SQL表:

CREATE TABLE `tbldoctor` (
  `D_ID` int(11) NOT NULL,
  `D_SPECILIZATION` varchar(255) NOT NULL,
  `D_NAME` varchar(255) NOT NULL,
  `D_GENDER` varchar(255) DEFAULT NULL,
  `D_ADDRESS` longtext DEFAULT NULL,
  `D_FEES` varchar(255) DEFAULT NULL,
  `D_CONTACTNO` varchar(255) DEFAULT NULL,
  `D_EMAIL` varchar(255) DEFAULT NULL,
  `D_USERNAME` varchar(255) NOT NULL,
  `D_PASSWORD` varchar(255) NOT NULL,
  `D_CREATIONDATE` timestamp NULL DEFAULT current_timestamp(),
  `D_LASTLOGIN` varchar(255) DEFAULT NULL
)

这是tblspecilization的表:

CREATE TABLE `tblspecilization` (
  `S_ID` int(11) NOT NULL,
  `S_NAME` varchar(255) NOT NULL,
  `S_CREATIONDATE` timestamp NOT NULL DEFAULT current_timestamp()
)

1 个答案:

答案 0 :(得分:0)

不要太复杂。有多种方法可以执行此操作,但是此echo "<option value="<?php echo htmlentities($results->D_ID);?>"><?php echo htmlentities($results->D_NAME);?></option> <?php";是错误的 不要混淆关闭,打开php标签,请尝试以下

<?php
include('incl/conn.php');
if(!empty($_POST['specilization'])) 
{
    $specilization =:$_POST['specilization'];

    $sql = "SELECT D_ID, D_NAME FROM tbldoctor 
            WHERE D_SPECILIZATION=:specilization";

    $query = $dbh->prepare($sql);
    $query->bindParam(':specilization',$specilization,PDO::PARAM_STR);
    $query->execute();
    $results=$query->fetchAll(PDO::FETCH_OBJ);
    $cnt=1;
    echo '<option selected="selected">Select Doctor</option>';
    if($query->rowCount() > 0) 
    {
        foreach($results as $results) 
            { 
            echo '<option value="'.htmlentities($results->D_ID).'">'. htmlentities($results->D_NAME).'</option>'; 
            }
    }
    else
    {
       echo '<option value=""> No Doctor in this specilization</option>';
       echo "<script>$('#submit').prop('disabled',true);</script>"; 
    }
}
?>