2 forms with the same name?
Moderator: General Moderators
2 forms with the same name?
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?
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?
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.:
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.
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
}Hope this helps someone else.
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?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" />
- kbrown3074
- Forum Contributor
- Posts: 119
- Joined: Thu Jul 20, 2006 1:36 pm
Do this^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.
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>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)?The Ninja Space Goat wrote:And 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>
Voila!
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...
And my solution allowed it to work whether keyboard was used or not... where is the confusion?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.