当用户单击带有 class 的按钮时,我正在尝试执行 PHP 代码来更新我的数据库中的条目deleteitem
。这是我的 AJAX/jQuery 代码:
<script type="text/javascript">
jQuery(document).ready(function() {
$(".deleteitem").click(function() {
var parent = $(this).closest('TR');
var id = parent.attr('id');
$.ajax({
type: "POST",
data: "id=" + id,
URL: "rejectRequest.php",
success: function(msg) {
$('#' + id).remove();
}
});
});
});
</script>
这是我的rejectRequest.php
文件
<?php
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASSWORD = '';
$DB_DATABASE = 'recomdb';
//Connect to mysql server
$con = mysql_connect($DB_HOST, $DB_USER, $DB_PASSWORD);
if (!$con) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db($DB_DATABASE);
if (!$db) {
die("Unable to select database");
}
$id1 = intval($_POST['id']);
if (isset($id)) {
$query = "UPDATE request SET status = 'rejected' WHERE id = $id1";
mysql_query($query) or die('Error, query failed');
}
mysql_close($con);
?>
JavaScript 工作正常:被删除。但是PHP不会执行。
当我捕获单击按钮时正在发送的内容时,它显示的内容如下:
Request URL:http://localhost/recom/students_requests_copy(2).php
Request Method:POST
Status Code:200 OK
Request Header
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:ar,en-US;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:4
Content-Type:application/x-www-form-urlencoded
Host:localhost
Origin:http://localhost
Referer:http://localhost/recom/students_requests_copy(2).php
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
id:2
Response Headersview source
Connection:Keep-Alive
Content-Type:text/html
Date:Sun, 29 Sep 2013 12:40:27 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.19
Transfer-Encoding:chunked
X-Powered-By:PHP/5.4.19
**
为什么不调用rejectRequest.php 文件?
**