dth62818 2017-09-05 23:20
浏览 29
已采纳

AJAX没有按预期工作

I've spent a day checking this code, but still can't find where the error is.

<div class="col-md-6">
  <br/>
  <label for="name">Class ID</label>
  <select class="form-control" id="csid" name="csid">
    <option>----------Please select a Class Code---------</option>
    <?php
      $query = $con->query("SELECT * FROM class WHERE class_status='Active' ");
      $rowCount = $query->num_rows;
      if($rowCount > 0) {
        while($row = $query->fetch_assoc()) { 
          echo '<option value="'.$row['class_id'].'">'.$row['class_code'].'</option>';
        }
      }
      else {
        echo '<option value="">Class ID not available</option>';
      }
    ?>
  </select>
</div>
<div class="col-md-6">
  <br/>
  <label for="name">Subject Name</label>
  <input type="text" class="form-control" name="subid" id="subid" disabled/>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script>
     $(document).ready(function(){
       $('#csid').change(function(){
         var classid = $(this).val();
         $.ajax({
           type:'POST',
           url:'ajax.php',
           data:{classid:classid},
           success:function(data){
             $('#subid').val(data);
           }
         });
       });
    });
</script>

And here is my ajax.php file:

<?php

include('dataconnect.php');

if (isset($_POST['classid'])) 
{
    $qry = "select * from class where class_id=". $_POST['classid'];
    $rec = mysql_query($qry);
    if (mysql_num_rows($rec) > 0) {
        while ($res = mysql_fetch_array($rec)) {
            echo $res['class_status'];
        }
    }
}

?>

Can anyone let me know where the error is, as I really can't find it. Thank you.

  • 写回答

1条回答 默认 最新

  • drsl90685154 2017-09-06 00:07
    关注

    If you are using PHP 7, your code won't have any output due to the fact that the mysql_ constructor is removed in PHP 7 (and deprecated as of PHP 5.5).

    Switching to either MySQLi or PDO will fix this problem.

    In addition to this, please ensure that you also use prepared statements to prevent SQL injection. :)

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

报告相同问题?

悬赏问题

  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码