Can you Post JSON in PHP form?
Last Post 12 Feb 2013 10:21 AM by dexum. 16 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
dexum
Veteran Member
Veteran Member
Posts:1158
Avatar

--
05 Feb 2013 02:00 PM

I have a PHP program that accepts input from a form and does some SQL stuff to build a table for user to validate info.

During the table build I add the data to an array and then at end use json_encode to put the array into json. I checked data with print_r.

The I create a form with a hidden field for json data and submit.

I am not able to get the json data to json_decode.

I get an error Undefined index: jsondata which I believe tells me the json data was not received.

Below is relevant code:

$jsondata = json_encode($drivers);
//print_r($jsondata); // Check for properly formed json data

If (!isset($_POST['Add']))  
{

?>
<form> action="" method="POST">
    
     
</form>



Is this possible or do I need to use the JQuery post method?

I have been reading about JQuery post and I am using JQuery date picker and a JQuery grid in this program already. I was just hoping there was an easier way than JQuery post.

TIA Danny

 

Danny Exum Model 515 OS Level 6.1
Chris Hird
Veteran Member
Veteran Member
Posts:2276

--
06 Feb 2013 09:34 AM
Danny

Your program does not make sense? I assume that the script calls itself and you are looking to convert $drivers to encoded json? But $drivers is not defined? Maybe you are trying to take the content of $_POST['driver']? You also echo $jsondata after assigning it $drivers via the json_encode() to the form which will always be NULL??
As the $_POST['driver'] field is hidden no user input will ever be collected?

Sorry but I have no idea what you are trying to achieve, but from what I see your code will never be able to decode any data because none will ever be assigned??

Chris..
Chris Hird Shield Advanced Solutions Ltd Home of JQG4i, HA4i and DR4i. http://www.shieldadvanced.com
dexum
Veteran Member
Veteran Member
Posts:1158
Avatar

--
06 Feb 2013 10:43 AM

Chris Thanks

 

I have a php program that user inputs up to 10 truck drivers codes and selects a date  using Jquery date picker.

Then user clicks submit button

Using SQL I pull driver name,  and Log Id from the Driver Master PF and date from date picker and load a grid using Jquery.

As I load the grid I am placing driver data in to an array.

At the top of the grid is an add button and a back button.

User validates grid data for the 10 drivers or less and clicks add or back button.

All of the above is working correctly.

On clicking the add button I want to add the records in the array to the Violations PF.

After Goggling and  reading for days I decided to place the grid data (that I placed in an array) in a json object.

My problem is how to post the json object to the same URL after user clicks add button.

I believe I know how to decode the json so that I can use SQL to add records to file.

I was not sure if I could just post the data and then using $Post_ retrieve it or if I would have to use a Jquery AJAX Post method.

I am lost as to the method to use and how to go about it.

 Any input to point me in right direction would be appreciated.

Thanks again.

PS

 Quote "I assume that the script calls itself and you are looking to convert $drivers to encoded json? " YES

 Quote "But $drivers is not defined?" I define all my variables at beginning on PHP code.

 Quote "Maybe you are trying to take the content of $_POST['driver']?"  Syntax may be wrong here. My understanding was I could not Post the Array I had to convert it. Is that right or Wrong.

Quote "You also echo $jsondata after assigning it $drivers via the json_encode() to the form which will always be NULL??"    ??? I did a print_r after json-encode and data looked good then I remarked out.

Quote "As the $_POST['driver'] field is hidden no user input will ever be collected?" I thought I found an example like this but I see what you are saying.

 

 

 

Danny Exum Model 515 OS Level 6.1
Chris Hird
Veteran Member
Veteran Member
Posts:2276

--
06 Feb 2013 03:01 PM
Danny

Never used JSON and while it should be pretty simple (according to what I quickly read) it does have a few issues with the data received at the server? Sorry I am no help on this....

Chris...
Chris Hird Shield Advanced Solutions Ltd Home of JQG4i, HA4i and DR4i. http://www.shieldadvanced.com
dexum
Veteran Member
Veteran Member
Posts:1158
Avatar

--
06 Feb 2013 04:06 PM

Thanks any way.

 

How would you do it with out JSON? 

How would you take a PHP array and post it to the same url to use data again?

Danny Exum Model 515 OS Level 6.1
Chris Hird
Veteran Member
Veteran Member
Posts:2276

--
07 Feb 2013 06:27 AM
Danny

Maybe create a session variable that is an array? Then when I am finished with it destroy the content? I do not see any ability to change the data by the user in your code so it is pretty much static data? You might even be able to load JSON into a session variable in the same manner as you are using it in the form to POST it back?

Chris...
Chris Hird Shield Advanced Solutions Ltd Home of JQG4i, HA4i and DR4i. http://www.shieldadvanced.com
dexum
Veteran Member
Veteran Member
Posts:1158
Avatar

--
07 Feb 2013 07:11 AM
Thanks Chris have a good day.
Danny Exum Model 515 OS Level 6.1
dexum
Veteran Member
Veteran Member
Posts:1158
Avatar

--
08 Feb 2013 01:49 PM

Every thing that I have read says this should work but I can't seem to get it to.

The PHP manual says json_code "Returns a JSON encoded string on success or FALSE on failure."

In the following example a string will post but a json will not. Why?



   


 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

If (!isset($_POST['submit']))  
{
		echo ("Submit was not pressed!!!") ;
   		$testjson = json_encode($arr);
   		$teststring = 'Bob';
   		print_r($testjson);   // used to test data encode
	 ?>	
        <form> action="http://192.168.1.9:10088/jquery/jsontest.php" method="POST"> 	
			 
			 					
 	     
		</form> 
 
 
 

Before Submit button is clicked I have.
Submit was not pressed!!!{"a":1,"b":2,"c":3,"d":4,"e":5} 

After Submit clicked.
Submit was pressed!!!Bob


 

Danny Exum Model 515 OS Level 6.1
Chris Hird
Veteran Member
Veteran Member
Posts:2276

--
08 Feb 2013 02:08 PM
Dexum

Before you press the submit button look at the source of the page, this should show you the hidden field content. If it is empty then that is what is going to be posted and you can work from there onwards to determine what is wrong.
I will take you code and check it out myself as well but thats where I would start to look!

Chris...
Chris Hird Shield Advanced Solutions Ltd Home of JQG4i, HA4i and DR4i. http://www.shieldadvanced.com
Chris Hird
Veteran Member
Veteran Member
Posts:2276

--
08 Feb 2013 02:27 PM
Dexum

I changed the code and it works? here is the output.

Submit was pressed!!!stdClass Object ( [a] => 1 => 2 [c] => 3 [d] => 4 [e] => 5 ) Bob

There are a couple of errors in the code (or it could be this stupid forum that screws up the posted code..

One thing you do need to do is remove the double quotes from the hidden fields here is my code (if it formats correctly)

 
 <?php
 $arr = array(&amp;#39;a&amp;#39; => 1, &#39;b&#39; => 2, &#39;c&#39; => 3, &#39;d&#39; => 4, &#39;e&#39; => 5);
 if (!isset($_POST[&#39;submit&#39;])) {
        echo ("Submit was not pressed!!!") ;
        $testjson = json_encode($arr);
        $teststring = &#39;Bob&#39;;
        print_r($testjson);   // used to test data encode
     ?> 
        <form action="dexum.php" method="POST">   
        <input name="DATA1" value=&lt;?php echo $testjson;?> type="hidden">  
        <input name="DATA2" value=&lt;?php echo $teststring;?> type="hidden">                       
        <input name="submit" value="submit" type="Submit">
        </form>
<?php
}
else
{
        echo ("Submit was pressed!!!") ;
        $jsondata=json_decode($_POST&amp;#91;&amp;#39;DATA1&amp;#39;&amp;#93;); // This does not work
        print_r($jsondata); // used to test json post
         
        $string=$_POST&amp;#91;&amp;#39;DATA2&amp;#39;&amp;#93;; // This works
        print_r($string);   // used to test string post
}
?> 

Chris Hird Shield Advanced Solutions Ltd Home of JQG4i, HA4i and DR4i. http://www.shieldadvanced.com
Chris Hird
Veteran Member
Veteran Member
Posts:2276

--
08 Feb 2013 02:30 PM
Dexum

This forum SUCKS! If you just remove the double quotes you code should work! As I said when you look at the page before you press the submit button it does show you what is going wrong. I cut and paste your code and had to remove some STUPID additional elements this STUPID forum adds. Sorry but they really screwed up this forum software.

Chris...
Chris Hird Shield Advanced Solutions Ltd Home of JQG4i, HA4i and DR4i. http://www.shieldadvanced.com
dexum
Veteran Member
Veteran Member
Posts:1158
Avatar

--
08 Feb 2013 03:12 PM

Chris you are the man.

It was the Double quotes.

Thanks again

Danny Exum Model 515 OS Level 6.1
dexum
Veteran Member
Veteran Member
Posts:1158
Avatar

--
08 Feb 2013 03:17 PM

Chris you are the man.

It was the Double quotes.

Thanks again

Working Code  for future reference. I hope it post OK.



   


 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

If (!isset($_POST['submit']))  
{
		echo ("Submit was not pressed!!!") ;
   		$testjson = json_encode($arr);
   		$teststring = 'Bob';
   		print_r($testjson);   // used to test data encode
	 ?>	
        <form> action="http://192.168.1.9:10088/jquery/jsontest.php" method="POST"> 	
		 type="hidden"> //removed quotes	 
		 type="hidden"> // removed quotes	 					
 	     
		</form> 
 
 
 


Danny Exum Model 515 OS Level 6.1
dexum
Veteran Member
Veteran Member
Posts:1158
Avatar

--
08 Feb 2013 03:18 PM
Forum still messed up code. Oh Well.
Danny Exum Model 515 OS Level 6.1
James Martin
New Member
New Member
Posts:6

--
09 Feb 2013 11:13 AM

I read this post and wanted to contact you.

I am using PHP, jQuery, ajax , JSON in development for web apps for System i(iSeries)

I would like to share information with you if your interested.

I am using the new TookKit Api by IBM & Zend are you using this new TookKitApi?

I also use a IDE WebSmart php

 

WebSmart PHP

WebSmart PHP is the fastest way for RPG, PHP and other programmers to develop IBM i and multi-platform PHP web applications. Its templates and wizards guide you through creating professional looking Inquiry, Transaction Processing, and other applications in under 5 minutes.

Let me know if you are interested?

Have a great day.

James

 

James R. Martin
J. Martin Associates
5185 Tara Ln., Bossier City, LA 71111 - 7810
Phone: (318) 752-4109 Cell Phone: ( 318) 773-9484
Personal e-mail:
jmartin@jmartinassociates.net


Scott Klement
Editorial Staff Member
Editorial Staff Member
Posts:16354
Avatar

--
10 Feb 2013 06:42 AM

Too bad I no longer have moderator powers, as this is in clear violation of forum rules.  You cannot advertise your services here. 

If you want to advertise, you need to contact iPro Developer's ad sales department, and pay for it.

dexum
Veteran Member
Veteran Member
Posts:1158
Avatar

--
12 Feb 2013 10:21 AM

I also use a IDE WebSmart php

 

LOL I would hope so being your an employee.
Danny Exum Model 515 OS Level 6.1
You are not authorized to post a reply.

Acceptable Use Policy