简体   繁体   English

尝试使用jquery和ajax填充下拉列表

[英]Trying to populate a drop down list with jquery and ajax

Here's my code :- 这是我的代码:-

  <script>
           $(document).ready(function(){                               //#This script uses jquery and ajax it is used to set the values in
           $("#day").change(function(){             //# the time field whenever a day is selected.

           var day=$("#day").val();
           var doctor=$("#doctor").val();

           $.ajax({
                 type:"post",
                 url:"time.php",
                 data:"day="+day+"&doctor="+doctor,
                 dataType : 'json', 
                 success:function(data){
                            var option = '';
            $.each(data.d, function(index, value) {
                option += '<option>' + value.res + '</option>';
                });
            $('#timing').html(option);
                             }

                  });

                  });

                 });
   </script>

And here's the php script. 这是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);

    $i = 0;                                 //Initialize the variable which passes over the array key values

    $row = mysqli_fetch_assoc($result);    //Fetches an associative array of the row
    $index = array_keys($row);             // Fetches an array of keys for the row.

    while($row[$index[$i]] != NULL)
    {

        if($row[$index[$i]] == 1) {
            $res = $index[$i];              
            echo json_encode($res);

        }
        $i++;
    }       



  ?>

I want options with time values inserted inside a select on my html page which looks something like this :- 我想在我的html页面的select中插入带有时间值的选项,它看起来像这样:-

  <select id="timing" name="timing"></select>

My java script code is posting values to the php script alright but the code is still not working. 我的Java脚本代码正在将值发布到php脚本,但是该代码仍然无法正常工作。 There aren't any errors in my javascript as I see it. 我所看到的JavaScript没有任何错误。 Kindly help me out 请帮我

      var postUrl = "time.php";
      $.ajax({
            type: "POST",
            url: postUrl,
            data: {day: day,doctor: doctor},
            dataType: "json",
            success: function (data) {
                $.each(data, function (key, value) {
                    $('#timing').append('<option value="' + key + '">' + value + '</option>');
                });
            }
        });

hope it's help to you 希望对你有帮助

   success:function(data){
           var select= '<select>';
           var option = '';
            $.each(data.d, function(index, value) {
                option += '<option>' + value.res + '</option>';
            });
           select = select+option'</select>';
           $('#timing').html(select);
      }

HTML : HTML:

<div id="timing"> </div>
    var day=$("#day option:selected").val();
    var doctor=$("#doctor option:selected").val();

    data:"{day:'"+day+"', doctor: '" + doctor + "'}" ,  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM