I'm trying to send a JSON object to PHP through ajax. But am getting null value on PHP side.
Jquery code
t_data = [{"name":"John", "age":30, "car":null},{"name":"John", "age":30, "car":null}]
$.ajax({
url: "someupdate",
data: {"test":JSON.stringify(t_data)},
dataType: "json",
type: "POST",
beforeSend: function () {
startLoading('Some Update in progress, please wait');
},
complete: function () {
stopLoading();
},
success: function (data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
}
PHP Side
$request = $this->getRequest();
if ($request->isPost()) {
$configDetails = $this->params()->fromPost('test');
var_dump($configDetails);
var_dump(json_decode($configDetails);
Output
string(153) "[{"name":"John","age":30,"car":null},{"name":"John","age":30,"car":null}]" NULL
As you can see in the output the response is visible in the required format when am not using json decode. I wonder why it showing null value when I decoded.
Could anyone kindly help me to identify what is missing my implementation for getting the result as expected
#UPDATE
- I tried without JSON.stringify and got below as the output
array(4) { [0]=> bool(false) [1]=> bool(false) [2]=> bool(false) [3]=> bool(false) }
- Someone could kindly suggest a betterway for passing the JSON object from Jquery to PHP (Zend Framework is what am using)
- I'm actually creating an object using
function createJSON() {
jsonObj = [];
$("input[class=email]").each(function() {
var id = $(this).attr("title");
var email = $(this).val();
item = {}
item ["title"] = id;
item ["email"] = email;
jsonObj.push(item);
});
console.log(jsonObj);
}