javascript - Trying to populate a drop down list with jquery and ajax - Stack Overflow

Here's my code :- <script>$(document).ready(function(){ #Thi

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.

  <?
    $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 :-

  <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. There aren't any errors in my javascript as I see it. Kindly help me out

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.

  <?
    $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 :-

  <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. There aren't any errors in my javascript as I see it. Kindly help me out

Share Improve this question asked Sep 24, 2013 at 10:40 AjitAjit 6752 gold badges15 silver badges27 bronze badges 8
  • i think you have put all code in javascript – Ravi Kavaiya Commented Sep 24, 2013 at 10:43
  • Can you show us the structure of data you are getting in success callback? – Ashis Kumar Commented Sep 24, 2013 at 10:44
  • I know variables are going through to the php script. It pops up in the console as I change the value from my 'day' dropdown list on my main page. – Ajit Commented Sep 24, 2013 at 10:48
  • @SilverBlade how can I see the data structure could you tell me? I am newbie :) – Ajit Commented Sep 24, 2013 at 10:49
  • use console.log(data); – Deepak Ingole Commented Sep 24, 2013 at 10:50
 |  Show 3 more ments

3 Answers 3

Reset to default 3
      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 :

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745136413a4613222.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信