Can someone direct me to documentation on multiple forms on an html document and how to handle the resulting php variables?

What happens if I have identical variable names in each of the forms? Are all forms converted to variables or only the one with a submit button, etc., etc.??

    You can have 100 forms on a page with 100 submit buttons (one submit for each form). They can all have the exact same variable names. If you submit form 49, form 49 and it's variables will be the only thing passed onto the processing script. Same variable names don't matter in that case, since they're in different forms. If you had the same variable name within a form (besides arrays), then this would be a problem.

    Address: <input type="text" name="address[]">
    Address contd: <input type="text" name="address[]">

    The above is perfectly legal within one form since the [] specifies that it will be an array. However

    Address: <input type="text" name="address">
    Address contd: <input type="text" name="address">

    is not. It may not produce an error so to speak, but you will only get the value from the 2nd one.

    Hope this helps.

    Cgraz

      I did some testing and confirmed what you have sed here. What I was hoping for was the ability to get multiple forms into the scrip, perhaps as an array, so that fields from the other forms could be checked... sort of like the ability in java script to address any of the fields on the html document, but appreantly php does not support that.

        give an example on what you are trying to do.

          What I was trying to do was to have a html/php page with multiple forms on it

          ...
          <form name=form1>
          <input type=text name=text1...>
          </form>

          <form name=form2>
          <input type=text name=text1...>
          </form>

          and be able (in php) to see/address each of the input fields when ever a submit button was pushed in EITHER form. Sort of the way that java script can see all of the fields on an html page.

          Concluded I can't do that in php; on the form associated with the clicked submit button gets passed to php... other fields on the page "go away".

            so I assume you are setting the actions of these forms to the same file?

            here is some sample code:

            Multiple Forms HTML file

            
            <html>
            <head>
            <title>Untitled Document</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            </head>
            
            <body bgcolor="#FFFFFF" text="#000000">
            <form name="form1" method="post" action="doform.php">
              <input type="text" name="textfield">
              <input type="submit" name="Submit" value="Submit">
            </form>
            <p>&nbsp;</p>
            <form name="form2" method="post" action="doform.php">
              <input type="text" name="textfield">
              <input type="submit" name="Submit2" value="Submit">
            </form>
            <p>&nbsp;</p>
            <form name="form3" method="post" action="doform.php">
              <input type="text" name="textfield">
              <input type="submit" name="Submit3" value="Submit">
            </form>
            <p>&nbsp;</p>
            </body>
            </html>
            
            

            doform.php

            
            <html>
            <head>
            <title>Untitled Document</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            </head>
            
            <body bgcolor="#FFFFFF" text="#000000">
            <?
            echo "$textfield";
            ?>
            
            </body>
            </html>
            
            

              You are essentially correct but the problem is that you only see the fields associated with the form containing the submit button that you clicked on. The fields from the other forms are not passed!

                cant stick all the fields in one form?

                  If you submit form 49, form 49 and it's variables will be the only thing passed onto the processing script.

                  Each form must have it's own submit button for the values to get passed. If you need to use more than one form, then you will have multiple submit buttons. A person can only hit submit one form at a time.

                  It's not possible to pass those variables from x amount of forms if the person pushes submit on one of those x forms. (This means if you have 20 forms, you can only get the value from 1 of them, not all 20). If you need to get values from different fields, then just put them all in one form. What is it you're doing that requires you to use multiple forms yet get the values from all of them?

                  If you want them to have the same name, use an array.

                  Cgraz

                    a month later

                    can a form have 2 submit buttons which would point to 2 different action pages?

                    Edit: never mind, I created 2 forms both with whole sets of hidden fields/data, each with its own submit button, and basically formatted it in HTML to look like part of one grand thing.

                      a month later

                      One thing to consider.

                      A lot of times there is a need to do different things for the same form. Especially in a complicated forms.

                      I usually have a hidden field called "ACTION" to determine the wanted action for the form. It can be many thins, like "insert_into_database" or "delete_from_database".

                      Having two or more forms doesn't always fit there. There can be unnecessary duplication of data or even cases where they can't can't be duplicated.

                      So, of course Javascript is one solution and I have sticked to it, by changing the value of the ACTION field. But is there any other, better solutions? Should there be, if we ask W3C for example?

                      BTW, one nice application to create PHP forms is here:
                      http://www.visiomode.com/formmaker/

                        You can have as many forms on a page pointing to as many different pages as you want

                          as far as i can see (read this thread rather quickly😉 )
                          you are looking for a way to "tell" the script, from wich form the processed data is comming...

                          use e.g.<input type='hidden' name='fromForm' value='Form_X'>

                            19 days later

                            sfarog,

                            How did you do the following:

                            Edit: never mind, I created 2 forms both with whole sets of hidden fields/data, each with its own submit button, and basically formatted it in HTML to look like part of one grand thing.

                            I am getting desperate... I need to have 2 forms, and those values entered in the db. I'm leaning towards cookies, but I really don't want to do it.

                            arrgggg!!!!

                              sfarog,

                              How did you do the following:

                              quote:

                              Edit: never mind, I created 2 forms both with whole sets of hidden fields/data, each with its own submit button, and basically formatted it in HTML to look like part of one grand thing.

                              I am getting desperate... I need to have 2 forms, and those values entered in the db. I'm leaning towards cookies, but I really don't want to do it.

                              Well, you can POST (or GET) only one form, period.

                              I don't know your situation, but there are possible workarounds:
                              - Make only one form, with all the information in it, some of them in hidden fields, probably.
                              If you need to have two or more submit buttons, you can use Javascript to change the value of the variable that determines the next "action".
                              I would do this.

                              • Make two forms and make your own Javascript submit function, which fiddles the variables: get's the values from the other form.
                                I wouldn't do this :-)

                              • Make two forms and duplicate the information in them. This is probably usable sometimes, when the information doesn't change too much.

                                Write a Reply...