user1808995 user1808995 - 4 months ago 79
PHP Question

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;
?>

Answer

Use json_decode to decode a json array in PHP

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

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