1

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"])

2
  • you can not decode an array it has to be json Commented Feb 4, 2013 at 4:57
  • You trying to pass an array($_POST) instead you need to password a simple string. '{'email':'[email protected]'}' Commented Feb 4, 2013 at 5:19

2 Answers 2

1

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']);
Sign up to request clarification or add additional context in comments.

1 Comment

what returns null? $requestBody? $var? or $var['email']? You gotta be a little more specific
0

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..

2 Comments

how do i access the posted jason object do you know how you do this I getting confused or people re confusing me
How are you posting data from client-end, can you please share that code chuck? And let me know if you also tried $_POST['variable-name'].. Here variable-name contains that json string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.