0

I have a problem and do not know what the problem is. I have a javascript variable in my html. which is:

 var people = '{"names": ["Matthew", "Lucas", "Todd", "Roxie", "Kyle", "Ken", "Gideon"], "surnames": ["Patel", "Lee", "Ingram", "Richter", "Katayanagi", "Katayanagi", "Graves"]}'; 

I parse the variable on one function in my script and use it. Everything works fine.

var mydata = JSON.parse(people); 

But then I need to send the data to a php file, I send it by wrtting the data to a hidden input.

 var strObject = JSON.stringify(mydata); var replaced = strObject.replace(/\//g, ''); oFormObject = document.forms['theForm']; oFormObject.elements["jsonData"].value = replaced; 

After which I try to encode it in my decode.php using:

$obj = $_POST['jsonData']; json_decode($obj); $s = stripslashes($obj); var_dump($s); 

But when I do a var_dump($s) I get this output:

string(147) "{"names":["Matthew","Lucas","Todd","Roxie","Kyle","Ken","Gideon"],"surnames":["Patel","Lee","Ingram","Richter","Katayanagi","Katayanagi","Graves"]}" 

Thus, I cannot output the contents in $s.Any suggestions.Please let me know if you need more information. BTW, its a homework assignment and I am stuck with the last section.

4
  • json_decode returns the object / array. You need to assign the return to a variable e.g. $jsOb = json_decode($obj) Commented Sep 1, 2015 at 11:24
  • you need to fetch the json returned from json_decode($obj)! Commented Sep 1, 2015 at 11:25
  • You want to get the name surname pairs from that?? Commented Sep 1, 2015 at 11:30
  • This might help you out, stackoverflow.com/questions/15986235/… You should check the answer with html_entity_decode() Commented Sep 1, 2015 at 11:32

3 Answers 3

0

try saving json_decode($obj) to a variable and var_dump that something like

var $temp = json_decode($obj); var_dump($temp); 
Sign up to request clarification or add additional context in comments.

Comments

0

the answer already is in the comments, but since this is the answer, i just post it as an answer.
you need to work with the return value from json_decode(). json_decode($obj) doesn't change the content of the variable $obj by itself:

$obj = $_POST['jsonData']; $obj = json_decode($obj); $obj = stripslashes($obj); var_dump($obj); 

1 Comment

@niranjan, when I try it your way nothing gets outputed.
0

If you are looking for name surname pairs from json , you can try this

$a = '{"names": ["Matthew", "Lucas", "Todd", "Roxie", "Kyle", "Ken", "Gideon"], "surnames": ["Patel", "Lee", "Ingram", "Richter", "Katayanagi", "Katayanagi", "Graves"]}'; $b = json_decode($a); $c = $b->names; $d = $b->surnames; for ($i = 0;$i< count($b->names); $i++){ echo $c[$i]." ". $d[$i] ."<br>"; } 

14 Comments

It still does not work. when I try $obj = json_decode($obj); $obj = stripslashes($obj); var_dump($obj); I still get the same output
do it without stripslashes and replace var_dump() with what i have given there
when I try it your way nothing gets outputed.
Can I gv you my .php and .js files.its only two files, test it and see what am I doing wrong because I literally copy pasted your code but it does not work.
No js files required. Go to phpfiddle.org, select initial code, paste the code and runu can see the output.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.