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.

Jones
  • 33
  • 7
  • `json_decode` returns the object / array. You need to assign the return to a variable e.g. `$jsOb = json_decode($obj)` – Jonnix Sep 01 '15 at 11:24
  • you need to fetch the json returned from `json_decode($obj)`! – Jeff Sep 01 '15 at 11:25
  • You want to get the name surname pairs from that?? – Niranjan N Raju Sep 01 '15 at 11:30
  • This might help you out, http://stackoverflow.com/questions/15986235/how-to-use-json-stringify-and-json-decode-properly You should check the answer with html_entity_decode() – Szenis Sep 01 '15 at 11:32

3 Answers3

0

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

var $temp = json_decode($obj);
var_dump($temp);
akashBhardwaj
  • 159
  • 1
  • 13
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); 
low_rents
  • 4,481
  • 3
  • 27
  • 55
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>";
    }
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
  • It still does not work. when I try $obj = json_decode($obj); $obj = stripslashes($obj); var_dump($obj); I still get the same output – Jones Sep 01 '15 at 11:47
  • do it without stripslashes and replace var_dump() with what i have given there – Niranjan N Raju Sep 01 '15 at 11:51
  • when I try it your way nothing gets outputed. – Jones Sep 02 '15 at 06:57
  • 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. – Jones Sep 02 '15 at 07:28
  • No js files required. Go to http://phpfiddle.org/, select initial code, paste the code and runu can see the output. – Niranjan N Raju Sep 02 '15 at 07:30
  • Yes you are right it works.But here is something weird. when I use this code: $obj = $_POST['jsonData']; echo $obj; i get this output : {\"names\":[\"Matthew\",\"Lucas\",\"Todd\",\"Roxie\",\"Kyle\",\"Ken\",\"Gideon\"],\"surnames\":[\"Patel\",\"Lee\",\"Ingram\",\"Richter\",\"Katayanagi\",\"Katayanagi\",\"Graves\"]}. But if I change it to this: $obj = $_POST['jsonData']; $p = json_decode($obj); echo $p; there is no output. I am missing something dont know what. – Jones Sep 02 '15 at 07:37
  • I think something is wrong with my php compiler.I put this code on http://phpfiddle.org/ and I get an output, but when I take it to hotdocks nothing happens. Is this possible. Is there somewhere I can post php code for you to just see? – Jones Sep 02 '15 at 07:49
  • Wierd. I compared bot json data in http://text-compare.com/, there is a special character after gideon in names and katayanagi in surnames. And that i what is creating the problem. You can also cross check there. – Niranjan N Raju Sep 02 '15 at 08:03
  • can you provide the code where you are encoding the data?? – Niranjan N Raju Sep 02 '15 at 08:03
  • I saw the code, in view, dont do anything except json.stringify(), remove all other code there for encoding. – Niranjan N Raju Sep 02 '15 at 08:05
  • it still outputs nothing. What am I really missing? – Jones Sep 02 '15 at 08:07
  • Niranjan May you please have a look at these files and tell me what exactly am I doing wrong. Here are the links : http://www.filedropper.com/script_8 http://www.filedropper.com/index_11 http://www.filedropper.com/decode... Thanks in advanced. – Jones Sep 02 '15 at 08:14
  • change the sendData function like this – Niranjan N Raju Sep 02 '15 at 08:53
  • function sendData() { var strObject = people; oFormObject = document.forms['theForm']; oFormObject.elements["jsonData"].value = strObject; } – Niranjan N Raju Sep 02 '15 at 08:53