在线问题系统

时间:2016-10-16 13:57:19

标签: javascript php ajax

我正在PHP中创建一个在线问题系统。但是,为了更新数据,我需要使用AJAX将JS变量传递给PHP,但我无法这样做。这就是代码。



  <?php   
     

    $sql=mysql_query("SELECT * FROM techroom ORDER BY Id DESC");

 
    while($row=mysql_fetch_assoc($sql)){
 

      
        echo "<tr><th>{$row['Id']}</th>";
      
        echo "<th>{$row['Item']}</th>"; 
        echo "<th> {$row['Description']}</th>";
        echo "<th>{$row['Quantity left']}</th>";
      
        echo '<th><button id="issue'.$row['Id'].'" onclick="issue('.$row['Id'].')">Issue</button></th></tr>';
      
      
        }
        ?>
&#13;
&#13;
&#13;

这是JS函数和AJAX代码:

&#13;
&#13;
<script>
function issue(id){    
   
  var quantity=prompt("Quantity required?");
  if(quantity!=null){
   var n= prompt("No. of days required");
   if(n<=10){
        $.ajax(
{
    type: "POST",
    url: "/tech.php",
    data: quantity,id,
    success: function(data, textStatus, jqXHR)
    {
        console.log(data);
    }
});        
     }
    else{
      confirm("Max. limit is 10 days");
    } 
   }
      
}
</script>
&#13;
&#13;
&#13;

这是我要指向的文件:

&#13;
&#13;
<?php
include "header.php";
include "mysql_connect.php";
$id=mysql_real_escape_string(strip_tags($_POST("id")));
$q=mysql_real_escape_string(strip_tags($_POST("q")));
$sql=mysql_query("SELECT * FROM techroom ORDER BY Id DESC");
     while($row=mysql_fetch_assoc($sql)){
      mysql_query("UPDATE techroom SET $row['Quantity left']=$row['Quantity left']-$q WHERE $row['Id']=$id");
    }
   ?>
&#13;
&#13;
&#13;

请在第一次这样做的时候纠正我的错误。

0 个答案:

没有答案