I am trying to fetch the below json content. The output of the json is like this. i want the below json to convert to php array.
PHP
$requestBody = file_get_contents("php://input");
$requestData = json_decode($requestBody,true);
if($requestData){
$no = $requestData["no[]"];
} else {
$no = 0;
}echo $no;
previous page Html
<input name="no[]" type="checkbox"/>
The above code returns an empty array. How is it possible to convert the above JSON to php object array.
First, make sure you're posting your form data as JSON and not as a regular form. One way to do this is serializing your form as a JSON object and then send it through an ajax request:
var formData = JSON.stringify($("#myForm").serializeArray());
//AJAX POST request here
Then on your server code, $requestData
will contain a nice php array of all posted data, which means that if your "no
" form data is an array, you will have to iterate over its values, you cannot access them using $requestData['no[]']
:
foreach($requestData['no'] as $no) {
//do your stuff
}