简体   繁体   中英

Receiving and sending back json data in PHP

I am sending data from services.js coming in as a json encoded string in the following format to myPhp.php:

{"Name":"alpha"}

I want to gather this data and send back to the services.js as it came back in json form adding beta to the string as in:

{"Name":"alpha beta"}

myPhp.php

<?php
$a = $_POST[data];
$b = json_decode($a);
$c = $b.Name + "beta";
echo ($c);
?>
<?php
    $a = $_POST[data];
    $b = json_decode($a);
    $c = $b['Name'] . " beta";
    header('Content-Type: application/json');
    echo json_encode($c);
?>

The "." notation is not used in PHP to access / set properties - more like Javascript and you need to have quotes around the $_POST variable ~ unless data is defined as a constant. I think you could try something like this though.

<?php
    $b = json_decode( $_POST['data'],true );
    $b['name'].='beta';
    echo json_encode($b);
?>

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