很抱歉没有说明我的问题。其实我想在数据库中更新我的数据。 但现在的问题是,即使我试图选择批准或拒绝ajax仍然不会更新。 我是ajax的新手并尝试搜索网络,但我的代码仍有问题
这是我的php页面
<?php
$querysel = "SELECT * FROM tblinternapplication WHERE course_code = '{$course_codeapp}' ORDER BY student_id, 1 DESC " ;
$resultsel = mysql_query($querysel, $connection);
echo "<h2><div class=\"h_title\">Status still in pending</div></h2>";
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th scope=\"col\">Matric ID</th>";
echo "<th scope=\"col\">Company name</th>";
echo "<th scope=\"col\" width = \"200\">Job Scope</th>";
echo "<th scope=\"col\">Status</th>";
echo "<th scope=\"col\">Action</th>";
echo "</tr>";
echo "</thead>";
while($rowsel = mysql_fetch_array($resultsel)){
if($rowsel['status_approval'] == NULL){
$id = $rowsel['id'];
echo "<tr>";
echo "<tr>"."<td class=\"align-center\">".$rowsel['student_id']."</td>";
echo "<td class=\"align-center\">".$rowsel['company_name']."</td>";
echo "<td class=\"align-center\" width = \"200\">".$rowsel['job_scope']."</td>";
echo "<td class=\"align-center\">";
if($rowsel['status_approval'] != NULL){
if( $rowsel['status_approval'] == 0)
{
echo "Reject";
}
else
{
echo "Approve";
}
}
else
{ echo "Pending";
}
echo "</td>";
echo "<td class=\"align-center\"><select name=\"approve\"
onchange=\"getstatus(this.value)\">";
echo "<option value=\"\">Select status:</option>";
echo "<option value=\"1\">Approve</option>";
echo "<option value=\"0\">Reject</option>";
echo "</select>";
echo "</td>";
echo "</tr>";
}
}
echo "</table>";
这是我的jscript页面
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function getstatus(id, approve)
{
if (approve=="")
{
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","updatestatus.php?id=" + id + "&status=" + approve,true);
xmlhttp.send();
}
</script>
然后这是我的updatestatus.php
<?php require_once("../includes/session.php"); ?>
<?php require_once("sessioncourse.php"); ?>
<?php $course_codeapp = $_SESSION['course_code'] ; ?>
<?php confirm_logged_in(); ?>
<?php require_once("../includes/connection.php") ?>
<?php require_once("../includes/functions.php") ?>
<?php
$id = $_GET['id'];
$status =$_GET['status'];
$sql="UPDATE tblinternapplication set status_approval = $status WHERE id = $id ";
$result = mysql_query($sql);
?>
我工作了几天但问题仍然无法解决。希望可以有人帮帮我。我将感激你的帮助!
答案 0 :(得分:1)
对于php中的JS的preapre结果,使用json_encode()函数。使您的更新脚本像这样:
<?php require_once("../includes/session.php");
require_once("sessioncourse.php");
$course_codeapp = $_SESSION['course_code'] ;
confirm_logged_in();
require_once("../includes/connection.php");
require_once("../includes/functions.php");
$id = $_GET['id'];
$status =$_GET['status'];
$sql="UPDATE tblinternapplication set status_approval = $status WHERE id = $id ";
$result = mysql_query($sql);
$json = array();
while ($row = mysql_fetch_assoc($result)) {
$json[] = $row;
}
echo json_encode($json);
不要关闭php标签,否则你可能会添加额外的空间字符
在你的情况下使用jQuery ajax是很好的实践。 Mkae你这样编码:
<script type="text/javascript">
function getstatus(id, approve)
{
$.ajax({
'url': 'updatestatus.php',
'data': {"id": id, "status": approve},
'success': function (response) {
console.log(response);
//TODO: use server response
}
});
}
</script>
答案 1 :(得分:0)
echo "<td class=\"align-center\"><select name=\"approve\"
onchange=\"getstatus(this.value)\">";
在上面的那一行中,您只传递一个值。同时你的JS函数等待2个参数。目前,id
始终等于0或1,状态始终未定义。
假设您只需更改该行:
echo "<td class=\"align-center\"><select name=\"approve\"
onchange=\"getstatus("+ $id +", this.value)\">";
一个注意事项:
$sql="UPDATE tblinternapplication set status_approval = $status WHERE id = $id ";
除了你不应该再使用mysql_ *函数之外,上面的代码打开了sql注入。为了避免使用mysql_扩展进行sql注入,你应该这样做:
$sql="UPDATE tblinternapplication set status_approval = ".mysql_real_escape_string($status) ." WHERE id = " .mysql_real_escape_string($id);
查看mysql_real_escape_string
here的文档。此外,在该页面上阅读警告消息 - 它告诉您应该使用什么代替mysql扩展
答案 2 :(得分:0)
我建议使用IE或Chrome中的F12等开发人员工具或fiddler检查您发送给PHP代码的请求。如果您收到任何错误,可以在回复中轻松查看