Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode JSON stringify in a different php file

Tags:

json

php

<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;
?>
like image 308
Siri Avatar asked Sep 23 '13 10:09

Siri


2 Answers

Use json_decode to decode a json array in PHP

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

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

like image 152
trrrrrrm Avatar answered Sep 26 '22 01:09

trrrrrrm


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

<?php
$data = json_decode($_POST['data']);
print_r($data);
?>
like image 44
jeffery_the_wind Avatar answered Sep 26 '22 01:09

jeffery_the_wind