ONYX
ONYX

Reputation: 5859

json_decode return json data

Im just posting single data value like this '{'email':'[email protected]'}'

php file

$var = json_decode($_POST,true);
echo json_encode($var["email"]);

at this stage i just want to return the email address to get it working buts its giving me this error:

json_decode() expects parameter 1 to be string, array given in C:\wamp\www\buyme\include\getemailaddress.php on line 4

line 4 is the first line in my code

all i want to be able todo is access the email value and return it back in json_encode($var["email"])

Upvotes: 1

Views: 285

Answers (2)

Nishu Tayal
Nishu Tayal

Reputation: 20830

As you can read in PHP Manual $_POST, $_POST contains values in associative array. As in manaul too, it is :

An associative array of variables passed to the current script via the HTTP POST method.

So if you are sending any json string from client-end in any variable, Use that variable to read that json string like this.

$var = json_decode($_POST['emaildata'],true);
echo json_encode($var["email"]);

Please check, if it works for you..

Upvotes: 0

DiverseAndRemote.com
DiverseAndRemote.com

Reputation: 19888

If I got you correctly and your posting a json string you can do:

$requestBody = @file_get_contents('php://input');
$var = json_decode($requestBody, true);
echo json_encode($var['email']);

Upvotes: 1

Related Questions