Ajax不会执行位于另一个文件中的php代码

时间:2013-09-29 12:50:54

标签: php jquery ajax

当用户点击类deleteitem的按钮时,我正在尝试执行PHP代码来更新数据库中的条目。这是我的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文件?

**

3 个答案:

答案 0 :(得分:2)

url的参数名为url(小写),而不是URL(大写)。

答案 1 :(得分:1)

在ajax参数中,url部分应为小写:网址,而不是网址

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();
            }
        });
    });
});

答案 2 :(得分:0)

改变这个:

if (isset($id)) {

进入这个:

if (isset($id1)) {

如果您使用的是过时的mysql,请使用mysqli或PDO。此外,您很容易使用SQL注入。