1

I am trying to use JQuery AJAX to allow the user to input information into a form in a pop-over box, and then the information gets added to a MySQL database. However, for some reason, when I use the JQuery AJAX functions -- I've tried both $.post() and $.ajax() -- to POST the data, it does not get POSTed. 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?

My JQuery AJAX function call when the user clicks to POST information is:

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

Or I have also tried:

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

My code for processNewAssignments.php (the URL the data is POSTed to) is:

<?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);

?>

user1516849
  • 55
  • 1
  • 8
  • [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 May 07 '13 at 00:20

2 Answers2

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.

Matanya
  • 6,233
  • 9
  • 47
  • 80
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'
hjpotter92
  • 78,589
  • 36
  • 144
  • 183