Two Forms on the Same PHP page

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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Two Forms on the Same PHP page

Post by neophyte »

How do you set up form tags for two or more forms on the same php page? How does that affect accessing the $_POST variables?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

You can't.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You can give the forms a name, and since you can only submit one form at a time the variable names can be the same - you just need identify which form was sent.

Is this what you want?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

DOH

Post by neophyte »

Yeah, exactly. How do you do that?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Replied

Post by neophyte »

How do you set up the form tags with names and then identify which form is submitted?
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Code: Select all

<form type="post" action="action.php">
<input type=hidden name="Form1" value=1>
// rest of form with inputs etc
<input type="submit">
<form>

<form type="post" action="action.php">
<input type=hidden name="Form2" value=1>
// rest of form with inputs etc.
<input type="submit">
<form>
and

Code: Select all

<?php

if (isset($_POST['Form1']))
{
    // Form1 was sumbitted
}
else
{
    // Form 2 was submitted
}

?>
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You can name the forms with <form name="blah"> and then to identify which was sent you can either have a hidden field in each form that says which form it is.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Weird

Post by neophyte »

I got a strange browser phenom yesterday. I have to forms. Neither are named. In IE 5.5 on XP the action of the first form was used as opposed to the second form's action when the submit button for the second form was hit. I didn't have any problems with that on Mozilla with a Mac or Linux platform. Now that's weird. Or does anybody know what happened there?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I've read about this phenomena many moons ago also. I can to the fully recall the complete article about it, but it was while discussing various IE bugs.

I belive it falls under the issue about how good different browsers support the tag-standards. You know, the ever lasting discussion about:

Code: Select all

<p>text</p>
as opposed to

Code: Select all

text<p>
just to mention a very small example.

Don't take my words on it tho, might be taken out of context somewhere else. But if someone finds a good resource or article on it, do post a link here.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

or you could name the submit buttons differently and check wich one was submitted...
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

AS long as you close the forms before starting the next one you should be fine.

.:Forgot from last post:.
You could also use a javascript function to sort out what fields to send depending on what button is clicked.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

IDIOT

Post by neophyte »

Don't forget to close </form with a ">". DOH.
Post Reply