Can you post JSON data to the same URL (PHP Program)?

General discussion on PHP

Can you post JSON data to the same URL (PHP Program)?

Postby dexum on Thu Feb 07, 2013 5:42 pm

Can you post JSON data to the same URL (PHP Program)?

<html>
<head>
<title> </title>

</head>
<body>

<?php

$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

If (!isset($_POST['submit']))
{
echo ("Submit was not pressed!!!") ;
$jsondata = json_encode($arr);
print_r($jsondata); // used to test data encode

print("<form action=\"http://192.168.1.9:10088/jquery/jsontest.php?data_sent=".$jsondata."\" method=\"POST\">");
?>
<input type="Submit" name="submit" value="submit">
</form>

<?php
}
else
{
echo ("Submit was pressed!!!") ;
$data = json_decode($_POST['jsondata']); //get JSON data and place in array for use
$newarr = json_decode($data) ;
print_r($newarr); // used to test data post and decode
}
?>
</body>
</html>
dexum
 
Posts: 1
Joined: Thu Feb 18, 2010 3:13 pm

Re: Can you post JSON data to the same URL (PHP Program)?

Postby fenelon on Sun Mar 10, 2013 5:58 pm

Yes, but you've done it wrong.

First you made "data_sent" a GET value. You need to move it to a hidden input field so the data ends up in the POST array.

Code: Select all
<input type="hidden" name="data_sent" value="<?php echo $jsondata ?>"  />


Second, the decoding is all wrong. You've got this

Code: Select all
$data = json_decode($_POST['jsondata']); //get JSON data and place in array for use
$newarr = json_decode($data) ;


Nothing in the form was named 'jsondata', and you're running it through json_decode() twice.
This should work:

Code: Select all
$newarr = json_decode($_POST['data_sent']); //get JSON data and place in array for use
fenelon
 
Posts: 27
Joined: Sun Oct 04, 2009 7:23 pm
Location: Texas, USA


Return to PHP

Who is online

Users browsing this forum: No registered users and 1 guest