【问题标题】:Trying to populate a drop down list with jquery and ajax尝试使用 jquery 和 ajax 填充下拉列表
【发布时间】:2013-09-24 10:40:51
【问题描述】:

这是我的代码:-

  <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 id="timing" name="timing"></select>

我的 java 脚本代码可以将值发布到 php 脚本,但该代码仍然无法正常工作。正如我所见,我的 javascript 中没有任何错误。请帮帮我

【问题讨论】:

  • 我想你已经把所有的代码都放在了javascript中
  • 你能告诉我们你在成功回调中得到的数据结构吗?
  • 我知道变量正在进入 php 脚本。当我从主页上的“天”下拉列表中更改值时,它会在控制台中弹出。
  • @SilverBlade 我怎样才能看到数据结构,你能告诉我吗?我是新手:)
  • 使用console.log(data);

标签: javascript php jquery


【解决方案1】:
      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>');
                });
            }
        });

【讨论】:

    【解决方案2】:

    希望对你有帮助

       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>
    

    【讨论】:

      【解决方案3】:
          var day=$("#day option:selected").val();
          var doctor=$("#doctor option:selected").val();
      
          data:"{day:'"+day+"', doctor: '" + doctor + "'}" ,  
      

      【讨论】:

        猜你喜欢
        • 2012-11-08
        • 1970-01-01
        • 1970-01-01
        • 2022-12-19
        • 1970-01-01
        • 2013-05-18
        • 1970-01-01
        • 1970-01-01
        • 2011-08-18
        相关资源
        最近更新 更多