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

?>

1

2 Answers 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.

Sign up to request clarification or add additional context in comments.

2 Comments

That makes so much sense!! Thank you!
I'd also recommend using $_REQUEST instead of $_POST or $_GET @user1516849
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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.