0

I've tried the fixes for similar questions but nothing has worked for me.

Trying to send an array to PHP via AJAX when my submit button is pressed.

In the PHP file I get the error - Notice: Undefined index: data

HTML

 <form id="email-form" action="add-user.php" method="post">
       ...
 </form>

Javascript

var frm = $('#email-form');
frm.submit(function (ev) {
var cars = ["Saab", "Volvo", "BMW"]; //this is the array i want to send for purposes of this question
var jsonString = JSON.stringify(cars);
    $.ajax({
        type: frm.attr('method'),
        url: frm.attr('action'),
        data: {'data': jsonString},
        success: function () {
            alert('ok');
        }
    });
});

PHP (add-user.php)

$data = json_decode(stripslashes($_POST['data']));

foreach($data as $d){
echo $d;
}

Get same error when trying- data: {data: jsonString}

console.log(jsonString) shows me the array so I know it exists.

Any ideas as to what is causing undefined index error? Thanks in advance.

7
  • var_dump($_POST['data']) and see what's coming through. And note that if you DON'T have magic_quotes enabled, the stripslashes() will be highly likely to corrupt the JSON text by removing escape slashes that are required by the JSON syntax rules. Commented Feb 19, 2015 at 20:52
  • @MarcB var_dump($_POST['data']); get me this ... Notice: Undefined index: data in /Applications/MAMP/htdocs/Website/add-user.php on line 5 NULL. Noted on your 2nd comment. Once I figure out this error I'll adjust that. Commented Feb 19, 2015 at 21:06
  • Try using {data : jsonString} (note that I am removing the quotations around your data parameter) or 'data='+ jsonString in your ajax. Then do a print_r($_POST) in your php. What does that tell you? stripslashes() should not be used in this case. Like Marc said, that will just likely create problems for you. Might not affect your code.. but I would suggest seperating the names of your paramaters (data, for instance) from the paramters that are native of the method you are using ($.ajax() also uses a paramater called data). Just makes it clearer! Commented Feb 19, 2015 at 21:14
  • thanks for recommendation... with data: {data : jsonString} --> same error Commented Feb 19, 2015 at 21:21
  • @entiendoNull — Removing the quotes will make no difference. Strings are fine as property names in object literals. Manually constructing the string could break things since the data might include special characters. Commented Feb 19, 2015 at 21:23

1 Answer 1

2

When you submit the form, the submit event fires. This triggers the normal Ajax request. The submit function then finishes running and the form submits.

The browser leaves the current page, and discards the execution environment containing the event handler waiting for the response to the Ajax request.

The server side script receives the normal form data, but the form doesn't have a data field, so you are getting the undefined index error.

You need to prevent the normal form submission:

ev.preventDefault();

You should also make sure that the fields that are actually in the form can be handled by the server side code for when the JS fails.

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

1 Comment

Completely missed out on the info regarding the submit button... that makes sense :)

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.