Nymor Posted August 10, 2009 Share Posted August 10, 2009 Hi, I'm having real trouble getting past first base with PHP and Json and hope someone will be able to put me out of my misery. I been searching for what seems like days and have tried to replicate what I've seen on numerous examples - but seem to be getting nowhere fast. I create a object in JS and convert it to json with JQuery's $.toJSON. I they sent it to a PHP page with $.post('php/myphpfile.php', p_data, function(data){}) In Firebug the Post part of the Net panel for it looks ok - I assume it is? {"race_id": 1, "racer_name": "3name", "racer_no": 333, "start_delay": 0, "notes": "3note", "sdata": [{"db_sectorid": 1, "stime": 0}, {"db_sectorid": 2, "stime": 0}]} but I'm having trouble getting anything out of it on the PHP end. What would I need to, say, pick the "racer_name" value out of it - or the first "stime" within the "sdata" array? I've tried json_decode in various guises but with no luck. Any assistance to solve my hopelessness would be greatly appreciated. (I've been losing hair since I was 20 and can't afford to lose much more) Thanks in advance Regards Nymor Quote Link to comment https://forums.phpfreaks.com/topic/169671-solved-php-and-json-having-trouble/ Share on other sites More sharing options...
play_ Posted August 11, 2009 Share Posted August 11, 2009 You can use json_decode. <?php $json = '{ "race_id": 1, "racer_name": "3name", "racer_no": 333, "start_delay": 0, "notes": "3note", "sdata": [ { "db_sectorid": 1, "stime": 0 }, { "db_sectorid": 2, "stime": 0 } ] }'; $decode = json_decode($json); echo '<pre>'; print_r($decode); echo '</pre>'; /* OUTPUTS: stdClass Object ( [race_id] => 1 [racer_name] => 3name [racer_no] => 333 [start_delay] => 0 [notes] => 3note [sdata] => Array ( [0] => stdClass Object ( [db_sectorid] => 1 [stime] => 0 ) [1] => stdClass Object ( [db_sectorid] => 2 [stime] => 0 ) ) ) */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/169671-solved-php-and-json-having-trouble/#findComment-895221 Share on other sites More sharing options...
play_ Posted August 11, 2009 Share Posted August 11, 2009 Sorry, here's a better example $json = '{ "race_id": 1, "racer_name": "3name", "racer_no": 333, "start_delay": 0, "notes": "3note", "sdata": [ { "db_sectorid": 1, "stime": 0 }, { "db_sectorid": 2, "stime": 0 } ] }'; $decode = json_decode($json, true); echo '<pre>'; print_r($decode); echo '</pre>'; will output Array ( [race_id] => 1 [racer_name] => 3name [racer_no] => 333 [start_delay] => 0 [notes] => 3note [sdata] => Array ( [0] => Array ( [db_sectorid] => 1 [stime] => 0 ) [1] => Array ( [db_sectorid] => 2 [stime] => 0 ) ) ) You know have an array. You can work from there. // Will output '3name' echo $decode['racer_name']; // will output 0 echo $decode['sdata'][0]['stime']; Quote Link to comment https://forums.phpfreaks.com/topic/169671-solved-php-and-json-having-trouble/#findComment-895237 Share on other sites More sharing options...
Nymor Posted August 11, 2009 Author Share Posted August 11, 2009 Thanks for that play_ While I can get your examples to work I think the thing I can't work out is how to get the data as sent to the page via $.post. In other words what would I need to do to to set the $json in your example to the contents of the $.post call as opposed to defining it as you have done within php. Sorry I'm being think here - it must be something ultra simple but I just can't see it. (I'm assuming that the $.post call is correct as Firebug gives me the data I printed in th OP). regards Nymor Quote Link to comment https://forums.phpfreaks.com/topic/169671-solved-php-and-json-having-trouble/#findComment-895292 Share on other sites More sharing options...
play_ Posted August 11, 2009 Share Posted August 11, 2009 Anytime. To be honest, i only know of one day, and I've only had to do this once. This is what I did. In your jQuery script, you will need to set up that json data as a string. this {"race_id": 1, "racer_name": "3name", "racer_no": 333, "start_delay": 0, "notes": "3note", "sdata": [{"db_sectorid": 1, "stime": 0}, {"db_sectorid": 2, "stime": 0}]} Would become var jsonData = '{"race_id": 1, "racer_name": "3name", "racer_no": 333, "start_delay": 0, "notes": "3note", "sdata": [{"db_sectorid": 1, "stime": 0}, {"db_sectorid": 2, "stime": 0}]}' Which might be a hassle because you'll have to build that string by concatenating literal strings and variables, example: var jsonData = '{"race_id": ' + raceID + ', "racer_name": ' + nameVar +' + racer_no": ' + racerNo, "start_delay": ' + delay + ' ... etc Then, in your php script, you'd do $data = $_POST['jsonData']; $decoded = json_decode($data)... etc. hope it helps! Quote Link to comment https://forums.phpfreaks.com/topic/169671-solved-php-and-json-having-trouble/#findComment-895370 Share on other sites More sharing options...
play_ Posted August 11, 2009 Share Posted August 11, 2009 Of course my answer above is only if you needed the data to keep that structure. If you can have a one-dimensional array, you could simply do (pretending your data is only: {"race_id": 1, "racer_name": "3name"} ): $raceID = $_POST['race_id']; $racer_name = $_POST['racer_name']; Quote Link to comment https://forums.phpfreaks.com/topic/169671-solved-php-and-json-having-trouble/#findComment-895372 Share on other sites More sharing options...
Nymor Posted August 11, 2009 Author Share Posted August 11, 2009 Still can't get this going ... The javascript side isn't too bad as I'm using $.toJSON from the jquery.json library so I've changed that to (some JS to create rdata - which all seems ok) var p_data = $.toJSON(rdata); var jsonData = "'" + p_data + "'"; $.post("php/racer_set.php", {jsonData: jsonData}); jsonData Posts as (from FireBug) jsonData '{"race_id": 1, "racer_name": "3Name", "racer_no": 333, "start_delay": 0, "notes": "3Notes", "sdata": [{"db_sectorid": 1, "stime": 0}, {"db_sectorid": 2, "stime": 0}]}' The php I've got is:- <?php $data = $_POST['jsonData']; $decode = json_decode($data, true); echo '<pre>'; print_r($decode); //echo $decode['racer_name']; //echo $decode['sdata'][0]['stime']; echo '</pre>'; ?> but the response I get is just <pre></pre> I've also tried $.post("php/racer_set.php", jsonData); $.post("php/racer_set.php", p_data); .... so without the extra 's $.post("php/racer_set.php", {jsonData: p_data}); but also with no luck. (I wish I could use a simpler format which I have working happily on other pages but in this case I want to add a record to MySQL and add related records with the new auto ID within the same php page/call. As I say this is - ... sorry. Regards Nymor Quote Link to comment https://forums.phpfreaks.com/topic/169671-solved-php-and-json-having-trouble/#findComment-895451 Share on other sites More sharing options...
RichardRotterdam Posted August 11, 2009 Share Posted August 11, 2009 but the response I get is just <pre></pre> Maybe you have magic quotes on try removing the slashes with strip slashes <?php $data = $_POST['jsonData']; // remove slashes when magic quotes is on $decode = (get_magic_quotes_gpc()==1) ?json_decode(stripslashes($data), true) :json_decode($data, true); // output the array echo '<pre>',print_r($decode),'</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/169671-solved-php-and-json-having-trouble/#findComment-895464 Share on other sites More sharing options...
Nymor Posted August 11, 2009 Author Share Posted August 11, 2009 magic quotes seem to have been the issue. <pre>Array ( [race_id] => 1 [racer_name] => 3Name [racer_no] => 333 [start_delay] => 0 [notes] => 3Notes [sdata] => Array ( [0] => Array ( [db_sectorid] => 1 [stime] => 0 ) [1] => Array ( [db_sectorid] => 2 [stime] => 0 ) ) ) 1</pre> No sure what the 1 is on the last line but I'm not going to worry about that - maybe standard? Didn't need the extra 's in the JS to get the above - used $.post("php/racer_set.php", {jsonData: p_data}); I can also get to the individual elements using the examples play_ posted. Many thanks to you both - your help, and patience, are very much appreciated. Kindest regards Nymor Quote Link to comment https://forums.phpfreaks.com/topic/169671-solved-php-and-json-having-trouble/#findComment-895481 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.