2017-09-05 165 views
0

我花了一天時間檢查此代碼,但仍找不到錯誤的位置。AJAX不按預期方式工作

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

這裏是我的ajax.php文件:

<?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']; 
     } 
    } 
} 

?> 

任何人都可以讓我知道錯誤所在,因爲我真的無法找到它。謝謝。

+1

對不起,但我不明白你遇到的問題。 –

+0

您是否嘗試過[**調試**](https://en.wikipedia.org/wiki/Debugging)您的代碼? –

+4

另外,請注意'mysql_'構造函數是[PHP5.5以下版本不推薦使用](https://wiki.php.net/rfc/mysql_deprecation),並且[**在PHP 7 **中被移除] (https://wiki.php.net/rfc/remove_deprecated_functionality_in_php7#extmysql)。請考慮切換到[** MySQLi **](http://php.net/manual/en/book.mysqli.php)或[** PDO **](http://php.net/manual/ en/book.pdo.php),確保你也使用[** prepared statements **](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)來防止[** SQL注入**](https://en.wikipedia.org/wiki/SQL_injection)。 –

回答