1

我正在尝试使用 JQuery AJAX 允许用户将信息输入到弹出框中的表单中,然后将信息添加到 MySQL 数据库中。但是,由于某种原因,当我使用 JQuery AJAX 函数(我已经尝试过 $.post() 和 $.ajax())来发布数据时,它不会被发布。相反,对于我尝试发布的所有字段,我都会收到“未定义索引”的错误消息(当我转到 processNewAssignments.php 时,这是我要发布到的 URL)。我究竟做错了什么?

当用户点击 POST 信息时,我的 JQuery AJAX 函数调用是:

$.ajax({url: "[THIS CONTAINS THE REST OF THE URL]/processNewAssignments.php",
                    type: 'POST',
                    data: {max_grade: maxGradeValue, title: taskNameValue, due: dueDateValue}});

或者我也试过:

$.post("[THIS CONTAINS THE REST OF THE URL]/processNewAssignments.php", 
        {max_grade: maxGradeValue, title: taskNameValue, due: dueDateValue});

我的 processNewAssignments.php 代码(数据发布到的 URL)是:

<?php session_start(); ?>

<!DOCTYPE html>
<html lang="en">
<head>
<title>Process Assignments</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<?php 

    $maxgrade = $_POST["max_grade"];
    $title = $_POST["title"];
    $due = $_POST["due"];
    $classID = $_SESSION["classID"];


    echo "hello!";
    echo $title . $classID . $due . $maxgrade;

    require("db.php");
    $query = "INSERT INTO assignments (title, assignmentID, classID, deadline, max_grade) VALUES ('$title', DEFAULT, '$classID', '$due', '$maxgrade')";
    $result = mysql_query($query, $db);

?>

4

2 回答 2

2

Instead I get error messages of "Undefined index" for all the fields I tried to POST (when I go to processNewAssignments.php, which is the URL I am POSTing to). What am I doing wrong?

If I understand correctly, you are trying to find your posted variables by sending a second GET request, i.e. entering the URL in the address bar.

If this is indeed the case, you SHOULD be getting undefined index, as you are not POSTing anything, hence the $_POST array is undefined.

To test your ajax POST request, you need to use Dev Tools (F12 on Chrome), choose the Network tab, and click on the post request that was made.

There you can see if the request was successful (200 response code), and what was the content of the response.

于 2013-05-07T00:42:39.847 回答
1

You have specified 5 columns to INSERT into; but have only 4 values passed to it:

title, assignmentID, classID, deadline, max_grade

and your values:

'$title', DEFAULT, '$due', '$maxgrade'
于 2013-05-07T00:21:47.137 回答