尝试使用jquery和ajax填充下拉列表 [英] Trying to populate a drop down list with jquery and ajax

查看:104
本文介绍了尝试使用jquery和ajax填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:-

  <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>

这是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++;
    }       



  ?>

我想要在我的html页面上的select中插入带有时间值的选项,它看起来像这样:-

I want options with time values inserted inside a select on my html page which looks something like this :-

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

我的Java脚本代码正在将值发布到php脚本,但是该代码仍然无法正常工作.我所看到的JavaScript没有任何错误.请帮我

My java script code is posting values to the php script alright but the code is still not working. There aren't any errors in my javascript as I see it. 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>');
                });
            }
        });

这篇关于尝试使用jquery和ajax填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆