Jab
Jab

Reputation: 129

Passing json object as url parameter

<?php  
  $jsonData = array(
    "comments" => "Fresh food",
    "container" => false,
    "cookedTime" => 2,
    "description" => "biryani",
    "refridgeration" => true,
    "serves" => 2,
    "veg" => true
);

json_encode($jsonData);
header("Location:Post.php?json=$jsonData");
?>

This is my php page which contains json object. I am passing this json object into another page Post.php.

<?php
$jsonData = $_GET['json'];
json_decode($jsonData, TRUE);
echo var_dump($jsonData);
?>

when I did a dump the result is C:\wamp\www\Hack\Post.php:16:string 'Array' (length=5). It is printing "Array" instead of the json object. What do I do?

Upvotes: 2

Views: 15396

Answers (3)

David Rojo
David Rojo

Reputation: 2394

As 1slock says you hace to encode the json but also add urlencode.

header("Location: Post.php?json=" . urlencode( json_encode($jsonData)) );

Upvotes: 5

1sloc
1sloc

Reputation: 1180

In your first code sample, you're not passing along the json_encoded value, but the array itself. Replace your last line with this, and skip the penultimate line:

header("Location: Post.php?json=" . json_encode($jsonData));

Upvotes: 0

Chris Cousins
Chris Cousins

Reputation: 1912

When you do json_encode you must have a variable to store the result:

$jsonData = json_encode($jsonData)

Without this, your data is still just a php object

Upvotes: 2

Related Questions