Page 1 of 1

2 forms with the same name?

Posted: Mon Jul 31, 2006 6:23 pm
by fgomez
Hello,

Does anyone know if it is considered poor practice to use two forms with the same name on one page? I haven't tried it; will it even work as expected?

I have a page with two forms on it. They have different names, but I need to add an extra button for the second form (let's call it form2), and it needs to be at the top of the page, above the first form (form1). I'm wondering: If I were to add another set of form tags and use the name form2, would the page know that what should be submitted is the values of the input tags in form2 further down the page?

I'm also thinking it might be easiest and least incorrect to combine all the forms into one form.

Any thoughts?

Posted: Mon Jul 31, 2006 6:28 pm
by bokehman
Use CSS and absolute positioning to take the button out of the document flow and place it at the top of the page. That way your HTML will be valid and your button where you want it.

Posted: Mon Jul 31, 2006 6:30 pm
by panic!
only the contents of the items within the form that has been submit()ed will be GET/POSTed to the server.

Posted: Tue Aug 01, 2006 10:28 am
by fgomez
If anyone ever gets tempted to combine their forms as I suggested (and did)--DON'T!!!

The way I've written my code, how the forms are processed is determined by which button is clicked, e.g.:

Code: Select all

if ($_POST['submit']) {
     //blah blah blah
} elseif ($_POST['update']) {
     //blah blah blah
} else {
     //blah blah blah
}
If you keep all the forms separate, this works great all the time (for me, anyway). If you combine them, it only works if the user actually clicks the button. If the user submits by pressing enter, you might as well ask them to hand-write their information and mail it to you.

Hope this helps someone else.

Posted: Tue Aug 01, 2006 10:30 am
by Luke
out a hidden value in there and check for that...

Code: Select all

<input type="hidden" name="sent" value="yes" />

Posted: Tue Aug 01, 2006 10:36 am
by fgomez
The Ninja Space Goat wrote:out a hidden value in there and check for that...

Code: Select all

<input type="hidden" name="sent" value="yes" />
That really amounts to the same thing. If $_POST['submit'] is set, $_POST['sent'] will always equal "yes"--but that doesn't tell me which form button the user should have clicked if they had clicked one instead of pressing enter. Ya dig?

Posted: Tue Aug 01, 2006 1:42 pm
by kbrown3074
What about using the same button name and changing the value based on what button they actually press?

Posted: Tue Aug 01, 2006 1:46 pm
by fgomez
kbrown3074 wrote:What about using the same button name and changing the value based on what button they actually press?
Yes, but what if they just press enter on their keyboard? Then they haven't pressed any button at all. I've changed my combined form back to multiple forms.

Posted: Tue Aug 01, 2006 1:55 pm
by Luke
bokehman wrote:Use CSS and absolute positioning to take the button out of the document flow and place it at the top of the page. That way your HTML will be valid and your button where you want it.
Do this^

Code: Select all

<form name="form1"><input type="hidden" name="form1sent" value="yes" /></form><form name="form2"><input type="hidden" name="form2sent" value="yes" /></form>
And this^

Voila!

Posted: Tue Aug 01, 2006 2:07 pm
by fgomez
The Ninja Space Goat wrote:

Code: Select all

<form name="form1"><input type="hidden" name="form1sent" value="yes" /></form><form name="form2"><input type="hidden" name="form2sent" value="yes" /></form>
And this^

Voila!
Just for the record, I've already solved my problem, and though I appreciate the feedback, I just don't get what you are trying to say here. Why would you create a hidden field to tell you if the form has been submitted when you could just check $_POST['submit'] (assuming that "submit" is the name of your submit button)?

Maybe I wasn't clear before: I had written my code so that if one button was clicked (e.g. submit1) the data was handled differently than if the other was clicked (e.g. submit2). Both buttons were part of the same form. To determine which button was clicked, I would check $_POST['submit1'] and $_POST['submit2']. And it all worked fine as long as the form was submitted by clicking a submit button. It was when the form was submitted via keyboard that things got a little unpredictable.

Hope that makes things clearer...

Posted: Tue Aug 01, 2006 2:10 pm
by feyd
When there is no submit button, assume the first button written out was clicked.

Posted: Tue Aug 01, 2006 2:10 pm
by Luke
fgomez wrote:Both buttons were part of the same form. To determine which button was clicked, I would check $_POST['submit1'] and $_POST['submit2']. And it all worked fine as long as the form was submitted by clicking a submit button. It was when the form was submitted via keyboard that things got a little unpredictable.
And my solution allowed it to work whether keyboard was used or not... where is the confusion?

Posted: Tue Aug 01, 2006 2:13 pm
by fgomez
Thank you, Feyd. I assumed there was a default but didn't think it was worth researching for this particular project. Will keep in mind for the future, though!

Posted: Tue Aug 01, 2006 2:15 pm
by Luke
that isn't the default... he was saying in your script... if no button was clicked, assume that the first one was.