简体   繁体   中英

Decode JSON stringify in a different php file

<script>
function getstaff(){
var staffinfo = $('#group').val();
var myJSONText = JSON.stringify(staffinfo);
    $.ajax({
    type:"POST",
    data:{data:myJSONText},
    url:"staffDetails.php",
    success: function(result){
    alert(result);
    }
});
}
</script>

How do I retrieve the posted data in staffDetails.php file. It some how gives me drastic errors. Can anyone suggest how exactly to retrieve what I sent to this php file

<?php
$data = JSON.parse($_POST['data'],',');
echo $data;
?>

Use json_decode to decode a json array in PHP

 $data = json_decode($_POST['data']);

http://php.net/manual/en/function.json-decode.php

http://php.net/manual/en/function.json-decode.php

<?php
$data = json_decode($_POST['data']);
print_r($data);
?>

Try this:

<?php

$data = json_decode($_POST['data'],true); 
print_f($data); // return data in associative array.

?>

In PHP there is function:

json_decode($_POST['data']);

use it.

JSON.parse 

is the function of Javascript so it will not work in php.

Read this: json_decode .

json_encode .

suppose you have array like

 `{"1":{"1":"Employer+EID","2":"File+Creation+Date","3":"File+Creation+Time","4":"Payer+EID","5":"Payer+QID","6":"Payer+Bank+Short+Name","7":"Payer+IBAN","8":"Salary+Year+and+Montd","9":"Total+Salaries","10":"Total+records","11":"","12":"","13":"","14":"","15":""}} 

you can read in php with :

$data = json_decode($_POST['data'],true);

echo $data[1][1]; use logic to cycle through data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM