Ajit
Ajit

Reputation: 675

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.

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

Upvotes: 1

Views: 9732

Answers (3)

PHP Dev
PHP Dev

Reputation: 503

      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>');
                });
            }
        });

Upvotes: 3

user2734846
user2734846

Reputation:

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

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

Upvotes: 0

Ravi Kavaiya
Ravi Kavaiya

Reputation: 849

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>

Upvotes: 0

Related Questions