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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SupraFast
    New Member
    • Sep 2008
    • 7

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

    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.

    Code:
    	
    foreach($_POST as $name => $value) {
          print "$name : $value<br>";
    }
    That code will output the hidden variables from both forms, rather than just the form the user submitted on respectively.


    Code:
    <form name = 'form0' method = 'POST' action='add.php'>
          <input type = 'hidden' name='hItem0' value = 'test0'>;
          <input type = 'submit' value = 'Add0'>
    </form>
    
    <form name = 'form1' method = 'POST' action='add.php'>
          <input type = 'hidden' name='hItem1' value = 'test1'>;
          <input type = 'submit' value = 'Add1'>
    </form>

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

    Someone care to shed some light?
  • drhowarddrfine
    Recognized Expert Expert
    • Sep 2006
    • 7434

    #2
    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.

    Comment

    • SupraFast
      New Member
      • Sep 2008
      • 7

      #3
      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.

      Comment

      • SupraFast
        New Member
        • Sep 2008
        • 7

        #4
        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.

        Comment

        • drhowarddrfine
          Recognized Expert Expert
          • Sep 2006
          • 7434

          #5
          Don't forget. I am never, ever, ever wrong.

          Comment

          Working...