1

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

8
  • i think you have put all code in javascript Sep 24, 2013 at 10:43
  • Can you show us the structure of data you are getting in success callback? 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
    Sep 24, 2013 at 10:48
  • @SilverBlade how can I see the data structure could you tell me? I am newbie :)
    – Ajit
    Sep 24, 2013 at 10:49
  • use console.log(data); Sep 24, 2013 at 10:50

3 Answers 3

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

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>
0
    var day=$("#day option:selected").val();
    var doctor=$("#doctor option:selected").val();

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.