0

我有一个脚本,它从脚本 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(){

我似乎也找不到错误。

4

3 回答 3

3

您需要在触发错误的行上方添加一个逗号:

dataType : 'json',
于 2013-09-24T07:04:09.230 回答
0

这是因为你在它上面的行上没有逗号......

于 2013-09-24T07:02:54.377 回答
0

很难说哪里出了问题,因为你把事情混在一起了。在 Javascript 端,您期望 JSON,但在 PHP 端,您生成 HTML。

使用 JSON 在服务器和浏览器之间发送数据。确保您实际生成有效的 JSON 并且仅生成 JSON。

这一行什么都不做(函数返回值,但不修改它)

json_encode($index[$i]);

这行没有意义 - 变量 $res 未初始化;

echo json_encode($res);
于 2013-09-24T07:41:44.503 回答