1

I'm using this html and javascript to send json data to my php file. http://jsfiddle.net/ExpertSystem/9aWNj/

How to access it from there in php to echo out a given element?

Something along the lines of:

$value = json_decode($_POST["newOrder"])
echo $value[1];

etc

I'm not sure how to retrieve data from this.

1
  • My code is here -> jsbin.com/igewiv/1/edit and hosted on a php server -> top50.zxq.net/fiddle.php. I'm trying just to be able to echo a selected element and their position i.e. 2 in position 1 or whatever it may be. so I know I can store them in a db etc. Commented May 10, 2013 at 13:06

7 Answers 7

1

Try this, It may helpful

$FP=fopen(JSON_DIR."JsonArray.txt",'r');
$J_ARRAY=fread($FP,filesize(JSON_DIR."JsonArray.txt"));
$J_ARRAY=json_decode($J_ARRAY,JSON_FORCE_OBJECT);
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a jsfiddle to see how the javascript should be in case you're still lost: http://jsfiddle.net/9aWNj/3/

this is your data decoded by php:

`stdClass Object ( [order] => Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 [7] => 9 [8] => 10 [9] => 11 [10] => 1 [11] => 12 ) )`

To access it it will be: example:

 $order_0=$value->order[0];
 $order_1=$value->order[1];

Or you use the true for json_decode and it will become like this

 $order_0=$value['order'][0];
 $order_1=$value['order'][1];

15 Comments

I've tried $value = json_decode($_POST["newOrder"], true); echo $order_0=$value['order'][0]; and not had any output from it, is there a problem with this code or a problem elsewhere?
you can't echo $order_0=$value['order'][0]; it should be 2 lines: $order_0=$value['order'][0]; echo $order_0;
I'm still not receiving any output from this. My code is here -> jsbin.com/igewiv/1/edit and hosted on a php server -> top50.zxq.net/fiddle.php. I'm trying just to be able to echo a selected element and their position i.e. 2 in position 1 or whatever it may be. so I know I can store them in a db etc.
can you update your question to show us what exactly are you doing please.
I don't understand, how is your php now?
|
0

try this :

$value = json_decode($_POST["newOrder"], true)
echo "<pre>";
print_r($value);

Note the second parameter true, that gives you output as array else it will be object.

Comments

0

To use json in PHP like I think you might want to then I suggest you put a true in the json_decode function.

This will give you the following:

$json = json_decode($data,true); 
$json["sub-values"]["sub thing"];

Comments

0

Why to send the JSON as a body of POST request? Do it simpler:

$.ajax({
    url: "<url_to_php_file>",
    type: "POST",
    data: { order: JSON.stringify(dataArr) }
});

And on the server side use:

$value = json_decode($_POST["order"]);
echo $value[1];

Comments

0

Check the php manual.

json_decode returns an object as default. use a second param true to return an array.

json_decode($_POST["newOrder"], true)

Comments

0

Json_decode return result as object by default, to get array you need to put second parameter to true.

$data = json_decode($_POST["newOrder"], false); return result as object

for printing object use

echo $data->something;

$data = json_decode($_POST["newOrder"], true); return result as array

for printing array use

echo $data['something'];

Comments

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.