2 forms with the same name?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

2 forms with the same name?

Post 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?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Post by panic! »

only the contents of the items within the form that has been submit()ed will be GET/POSTed to the server.
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

out a hidden value in there and check for that...

Code: Select all

<input type="hidden" name="sent" value="yes" />
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post 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?
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

Post by kbrown3074 »

What about using the same button name and changing the value based on what button they actually press?
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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!
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

When there is no submit button, assume the first button written out was clicked.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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?
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post 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!
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

that isn't the default... he was saying in your script... if no button was clicked, assume that the first one was.
Post Reply