I've been tearing my hair out at such a simple problem. I have the following JS array:
var orderDetailsArray = new Array();
orderDetailsArray[0] = 'test 1';
orderDetailsArray[1] = 'test 2';
orderDetailsArray[2] = 'test 3';
orderDetailsArray[3] = 'test 4';
Then I have the following Ajax code to send this array to a PHP file
$.ajax({
type: "POST",
url: 'http://testdomain.com/file.php',
data: JSON.stringify(orderDetailsArray),
contentType: "application/json",
success: function(data) {
alert(data);
}
});
In my PHP file I have the following
$orderDetailsArray = json_decode($_POST['orderDetailsArray']);
echo $orderDetailsArray[0];
But for some reason alert(data) always just returns blank. I have no idea why this doesn't return the correct values.
Any help would be really great.
Thanks
-
So do you want to send JSON or receive url encoded form data?Musa– Musa2014年08月28日 12:12:01 +00:00Commented Aug 28, 2014 at 12:12
8 Answers 8
You did not name your array in the client side before sending it, therefore the whole of $_POST is this array, and $_POST['orderDetailsArray'] is undefined.
You must name it client-side:
$.ajax({
type: "POST",
url: 'http://testdomain.com/file.php',
data: {
orderDetailsArray: JSON.stringify(orderDetailsArray)
},
contentType: "application/json",
success: function(data) {
alert(data);
}
});
1 Comment
data: { orderDetailsArray: JSON.stringify(orderDetailsArray)}
Comments
Your post data is not a key value pair, so you cant access in via key in php.
Either use the php input stream:
$orderDetailsArray = json_decode(file_get_contents('php://input'));
OR set a key in your ajax:
data: { orderDetailsArray: JSON.stringify(orderDetailsArray)}
2 Comments
You should declare array like this and then can directly pass it ajax. (No need to stringify)
var orderDetailsArray = {};
orderDetailsArray[0] = 'test 1';
orderDetailsArray[1] = 'test 2';
orderDetailsArray[2] = 'test 3';
orderDetailsArray[3] = 'test 4';
$.ajax({
type: "POST",
url: 'http://testdomain.com/file.php',
data: {'order_details':orderDetailsArray},
contentType: 'application/x-www-form-urlencoded',
success: function(data) {
alert(data);
}
});
4 Comments
without dataType
data: {orderDetailsArray :orderDetailsArray},
with dataType
dataType: "json",
data: JSON.stringify({orderDetailsArray:orderDetailsArray}),
4 Comments
well you cannot pass the value directly, you need to assign it to a "key" like this
data: {'arr':JSON.stringify(orderDetailsArray)},
and access the same at php side like this
$orderDetailsArray = json_decode($_POST['arr']);
Reference:
http://api.jquery.com/jQuery.ajax/
Happy Coding :)
Comments
The problem is in the data submitted. On the server side there's no data specified for
$_POST['orderDetailsArray']
Change your ajax to:
$.ajax({
type: "POST",
url: 'http://testdomain.com/file.php',
data: 'orderDetailsArray='+JSON.stringify(orderDetailsArray),
contentType: "application/json",
success: function(data) {
alert(data);
}
});
Comments
Try change the data: data: JSON.stringify(orderDetailsArray)
//by
data: {orderDetailsArrayData:orderDetailsArray}
or
data: JSON.stringify({orderDetailsArrayData:orderDetailsArray})
// in php
$orderDetailsArray = $_POST['orderDetailsArrayData'];
echo $orderDetailsArrayData[0];
or
$orderDetailsArray = json_decode($_POST['orderDetailsArrayData']);
echo $orderDetailsArrayData[0];