0

スクリプト php からオプションをフェッチして、メイン ページのドロップダウン リストに入力するスクリプトがあります。

ジャバスクリプトはこちら

   <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 on line に「}」が見つからないというエラーが表示されます

  $("#day").change(function(){

エラーも見つからないようです。

4

3 に答える 3

0

その上の行にカンマがないからです...

于 2013-09-24T07:02:54.377 に答える