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.
Aucun commentaire:
Enregistrer un commentaire