douyan8267 2014-05-08 11:19
浏览 75
已采纳

如何在jquery中编写PHP代码以从mysql数据库中检索值?

I am trying to get the values for the next drop-down from a database, which will be dependent on two previous drop-downs. The values for first two drop-downs are listed in the file itself. I want the second drop-down to be enable after selecting values from first, and similarly for third after second. Kindly help. HTML code below:

<form>
<select id="branch" name="branch" class="form-control" onchange="showUser(this.value)">
        <option value="">Select</option>
        <option value="1">Civil</option>
        <option value="2">Computer</option>
        <option value="3">Mechanical</option>
        <option value="4">Electronics and Telecommunications</option>
        <option value="5">Electrical and Electronics</option>
        <option value="6">Information Technology</option>
</select>
<select id="semester" name="semester" class="form-control" disabled>
      <option value="1">I</option>
      <option value="2">II</option>
      <option value="3">III</option>
      <option value="4">IV</option>
      <option value="5">V</option>
      <option value="6">VI</option>
      <option value="7">VII</option>
      <option value="8">VII</option>
</select>
</form>

jquery is:

<script>

     $(document).ready(function(){
      document.cookie = "s =hello" ;

     console.log('hello');

         $("#semester").attr("disabled", true);
         $("#branch").change(function(){
             $("#semester").attr("disabled", false); 

              $b = $('#branch').val();


              $("#semester").change(function(){
                $s = $('#semester').val();
                $("#sub_code").attr("disabled", false);
                console.log($s);

                if($s!=1||$s!=2)
                $s = $b+$s;

                <?php 

                 $s= $_COOKIE['s'];

                 $sql = "SELECT * FROM subjects WHERE sem_code=`$s`";

                ?>

             });   
         });
     });
     </script>

I did not run the query since it is not assigned properly yet.

  • 写回答

1条回答 默认 最新

  • dongpu6141 2014-05-08 12:07
    关注

    You can't include php code in javascript , the first is executed on the server side, the second is executed on the client side which means that you can't re-execute only if you resend a request to the server, obviously this is usually done by submitting forms, BUT sometimes -like in your case- we don't want to reload the whole page for each request ! and for this purpose we use AJAX ajax sends post/get request to a specified php page which does the desired server-side tasks (updating data in the database, selecting some results from database, dealing with sessions maybe, etc...) try something like this:

    var pathurl='/path/to/your/php/file';
    var params={}; //the parameters you want to send
    params['semester']=$s;
    var requestData= $.ajax({
            type: 'POST',
            url: pathurl,
            cache: 'false',
            data: params,
            dataType: "json",
            beforeSend: function () {
                //here you can begin an animation or anything...
            },
            complete: function () {
                //here you can stop animations ...
    
            },
            success: function (response) {
                console.log(response); //check your console to verify the response
                //loop other elements inside response
                $.each(response, function (index, resultArray) {
                //do something : in your case append to dropdown a new option
                });
            }
     });
       requestData.error(function () {
       alert("error");
    });
    

    you should create a php file with the path specified in the above code, and there you can extract what you need from the database table, store values in an array and finally use:

    echo json_encode($resultArray);
    

    hope this was helpful

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置