Connecting Tech Pros Worldwide Forums | Help | Site Map

2 Forms on 1 Page. How to separate Post Variables?

Newbie
 
Join Date: Sep 2008
Location: Brooklyn, NY USA
Posts: 7
#1: Sep 21 '08
I have 2 forms on one page. I have hidden variables in each form, with separate names, that contain values. The user decides which form to submit on. Only 1 form will be processed.

Now my problem is, when I read the Post Variables from the server side with PHP.

Expand|Select|Wrap|Line Numbers
  1.  
  2. foreach($_POST as $name => $value) {
  3.       print "$name : $value<br>";
  4. }
  5.  
That code will output the hidden variables from both forms, rather than just the form the user submitted on respectively.


Expand|Select|Wrap|Line Numbers
  1. <form name = 'form0' method = 'POST' action='add.php'>
  2.       <input type = 'hidden' name='hItem0' value = 'test0'>;
  3.       <input type = 'submit' value = 'Add0'>
  4. </form>
  5.  
  6. <form name = 'form1' method = 'POST' action='add.php'>
  7.       <input type = 'hidden' name='hItem1' value = 'test1'>;
  8.       <input type = 'submit' value = 'Add1'>
  9. </form>
  10.  

The output on add.php is test0 and test1, despite which form the user submitted on.

Someone care to shed some light?

drhowarddrfine's Avatar
Expert
 
Join Date: Sep 2006
Posts: 5,573
#2: Sep 21 '08

re: 2 Forms on 1 Page. How to separate Post Variables?


The html is correct, and functions correctly, so I can only assume there is something wrong on the PHP side. You need to ask on that board.
Newbie
 
Join Date: Sep 2008
Location: Brooklyn, NY USA
Posts: 7
#3: Sep 21 '08

re: 2 Forms on 1 Page. How to separate Post Variables?


Well the PHP side is the server side. There can't be anything wrong there because the HTML submits the post variables to the PHP side.

The PHP side doesn't create post variables...so they are obviously getting sent over.
Newbie
 
Join Date: Sep 2008
Location: Brooklyn, NY USA
Posts: 7
#4: Sep 21 '08

re: 2 Forms on 1 Page. How to separate Post Variables?


Well you are correct about the HTML being right. I stupidly, forgot to close the tag on the form. I put </form rather than </form>. I typed it correctly, of course, above.

It amazes me how many problems I run into due to stupid mistakes like this lol. I always look for a logical answer as to why something isn't work but it always winds up being the most unexpected mistake.

Thanks for your help.
drhowarddrfine's Avatar
Expert
 
Join Date: Sep 2006
Posts: 5,573
#5: Sep 22 '08

re: 2 Forms on 1 Page. How to separate Post Variables?


Don't forget. I am never, ever, ever wrong.
Reply