I want to send an array from my client jQuery code to my server PHP code. On the jQuery side, this is what I am doing:
$.ajax( {
url: someUrl,
data: {
action: 'opensearch',
search: query,
namespace: [10, 846],
suggest: ''
},
dataType: 'json',
});
On PHP's side:
echo $params['action'] ."\r\n";
echo $params['namespace'][0] ."\r\n";
echo $params['namespace'][1] ."\r\n";
The output of this is:
opensearch
0
So the parameters are sent correctly as I manage to print the string correctly. However the array appears not to sent correctly.
I have tried to use json_decode
with and without the true
option but to no avail.
Any suggestion is most welcome.
6 Answers 6
json_decode() converts valid JSON strings into objects. It accepts three parameters each of which is described below:
The JSON string itself.
Optional parameter assoc: By default this value is false. If changed to true, json_decode will convert objects to associative arrays
Depth: Maximum allowed depth of a recursive structure in the JSON string. It used to be 128 before PHP 5.3. PHP 5.3 has this limit increased to 512 bytes default. This parameter is also optional.
$objJson = json_decode($json);
Comments
$.ajax( {
url: someUrl,
type:'post'
data: encodeURIComponent('action=opensearch&search=query&namespace[]=10&namespace[]=846&suggest='),
dataType: 'json',
});
php
$params=json_decode($_POST);
echo $params->action;
1 Comment
The data is not being posted as JSON, it is being posted in a proper HTTP POST.
However, PHP will only convert it to an array of the name of the field is structured in such a way that indicates it is an array. Try replacing: namespace: [10, 846]
with "namespace[]": [10, 846]
Right now, jQuery is likely posting this: namespace=10&namespace=846
, which will not be properly converted to an array. Adding the square brackets should take care of that.
1 Comment
json_decode
or anything you may have. I was actually incorrect in telling you to add square brackets, jQuery will do that for you - however, you do not need to do any processing on your server side. Just var_dump($_POST)
and you will see the data you are looking for. I just tested this as well with jQuery.Try logging your received data and see what you have received, I have tested you ajax post using the following code, and it returns the value 10 as desired:
$data = $_REQUEST;
error_log($data['namespace'][0]."\n",3,"log.txt");
print_r($data['namespace'][0]);
Also use, error and success options to see what response you are getting from your php script
$.ajax( {
url: 'rec.php',
data: {
action: 'opensearch',
search: "abc",
namespace: [10, 846],
suggest: ''
},
dataType: 'json',
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(JSON.stringify(XMLHttpRequest));
console.log(JSON.stringify(textStatus));
console.log(JSON.stringify(errorThrown));
},
success: function (data) {
console.log(JSON.stringify(data));
}
});
});
1 Comment
You do not need to decode your $_POST
variable, actually, $_POST
already contain your data as you want them.
Just have a test, create a jsonpost.php with the following code :
<?php
if (count($_POST) > 0)
{
header("Content-type: text/plain; charset=utf-8");
echo nl2br(var_export($_POST, true));
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
</head>
<body>
<div id="target"></div>
<script type="text/javascript">
$.ajax( {
url: 'jsonpost.php',
type:'post',
data: {
action: 'opensearch',
search: 'test',
namespace: [10, 846],
suggest: ''
},
success: function(data) {
$('#target').html(data);
}
});
</script>
</body>
</html>
This will display :
array (
'action' => 'opensearch',
'search' => 'test',
'namespace' =>
array (
0 => '10',
1 => '846',
),
'suggest' => '',
)
Your main mistake is that you're thinking that dataType
will POST your data as json, but actually the dataType
content will define the EXPECTED data to get (to have an already-decoded object when calling your callbacks).
Comments
you are sending form data, so it's not going to be json_encoded.
$.ajax( {
url: someUrl,
data: {
json_string: JSON.stringify({
action: 'opensearch',
search: query,
namespace: [10, 846],
suggest: ''
})
},
dataType: 'json',
});
and then on the PHP side
$data = json_decode($params['json_string'], true);