Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/169671-solved-php-and-json-having-trouble/
Share on other sites

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
                )

        )

)



*/
?>

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'];

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

 

 

 

 

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!

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'];

 

 

 

 

:-[

 

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

 

 

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>'; 
?>

:)

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.