下拉值的 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>

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


<?php

include('incl/conn.php');

if(!empty($_POST['specilization'])) 

{

    $specilization =:$_POST['specilization'];


    $sql = "SELECT D_ID, D_NAME FROM tbldoctor 

            WHERE D_SPECILIZATION=:specilization";



子衿沉夜
浏览 153回答 1
1回答

慕码人2483693

不要让它太复杂。有多种方法可以做到这一点,但这echo "<option value="<?php echo htmlentities($results->D_ID);?>"><?php echo htmlentities($results->D_NAME);?></option> <?php"; 是错误的不要将自己与关闭,打开php标签混淆,尝试以下<?phpinclude('incl/conn.php');if(!empty($_POST['specilization']))&nbsp;{&nbsp; &nbsp; $specilization =:$_POST['specilization'];&nbsp; &nbsp; $sql = "SELECT D_ID, D_NAME FROM tbldoctor&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE D_SPECILIZATION=:specilization";&nbsp; &nbsp; $query = $dbh->prepare($sql);&nbsp; &nbsp; $query->bindParam(':specilization',$specilization,PDO::PARAM_STR);&nbsp; &nbsp; $query->execute();&nbsp; &nbsp; $results=$query->fetchAll(PDO::FETCH_OBJ);&nbsp; &nbsp; $cnt=1;&nbsp; &nbsp; echo '<option selected="selected">Select Doctor</option>';&nbsp; &nbsp; if($query->rowCount() > 0)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; foreach($results as $results)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<option value="'.htmlentities($results->D_ID).'">'. htmlentities($results->D_NAME).'</option>';&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;echo '<option value=""> No Doctor in this specilization</option>';&nbsp; &nbsp; &nbsp; &nbsp;echo "<script>$('#submit').prop('disabled',true);</script>";&nbsp;&nbsp; &nbsp; }}?>
打开App,查看更多内容
随时随地看视频慕课网APP