You could make two forms with 2 different actions

<form action="login.php" method="post">
    <input type="text" name="user">
    <input type="password" name="password">
    <input type="submit" value="Login">
</form>

<br />

<form action="register.php" method="post">
    <input type="text" name="user">
    <input type="password" name="password">
    <input type="submit" value="Register">
</form>

Or do this

<form action="doStuff.php" method="post">
    <input type="text" name="user">
    <input type="password" name="password">
    <input type="hidden" name="action" value="login">
    <input type="submit" value="Login">
</form>

<br />

<form action="doStuff.php" method="post">
    <input type="text" name="user">
    <input type="password" name="password">
    <input type="hidden" name="action" value="register">
    <input type="submit" value="Register">
</form>

Then you PHP file would work as a switch($_POST['action']) ... furthermore, they can't click on both links at the same time or make a simultaneous request, each submit is a separate request.

Your PHP would then go on with the switch logic or have different php files doing a login procedure then a registration procedure

Answer from cristi _b on Stack Overflow
🌐
Quora
quora.com › Can-two-forms-be-on-one-page-If-so-how-does-it-work-PHP
Can two forms be on one page? If so, how does it work (PHP)? - Quora
You might get more errors if you cram too much on a page. It is best to have one form per menu or button click. If it does make sense to have form variations like register as Google or register by email, then ...
Discussions

php - Two forms in one page - Stack Overflow
Hey guys i have the same form twice in the same page (i tried to read a lot of question with the same problem but that did not resolve my problem ...) my problem is that when i send one form that s... More on stackoverflow.com
🌐 stackoverflow.com
Need help with multiple forms on one page, here
This is a shopping cart update of some sort? Can u post a screenshot of the rendered html? Im having a hard time understanding why the entire thing isnt one form. More on reddit.com
🌐 r/PHPhelp
6
4
April 5, 2023
Multiple Forms on One Page - PHP - SitePoint Forums | Web Development & Design Community
Under each Article on my website, members can post Comments. And beneath each Comment, other members can give the Comment a rating. So if there are 30 Comments, I would have 30 Forms!! What is the proper way to set up my Forms and Submit buttons so they work properly? Here is an example… More on sitepoint.com
🌐 sitepoint.com
0
July 19, 2014
html - How to handle multiple php forms using one php page - Stack Overflow
I'm a student and I know nothing about PHP. but I have to do one of my assignment using PHP. Here is the problem which I faced. On my index page, there are 3 links that direct to 3 different forms. when the user chooses one form, then fill and submit it result.php file shows the output using ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Bavotasan
bavotasan.com › home › processing multiple forms on one page with php
Processing Multiple Forms on One Page with PHP - bavotasan.com
October 30, 2012 - <?php if (!empty($_POST['mailing-submit'])) { //do something here; } if (!empty($_POST['contact-submit'])) { //do something here; } ?> Now all you need to do is create your processes within those two “if” statements and each form will be dealt with accordingly when it it filled and submitted. Click to email a link to a friend (Opens in new window) Email
Top answer
1 of 2
1

You need to name your forms and handle them separately. You can move your form building code in a MailFormType class and create 2 named forms:

$form1 = $this->get('form.factory')
    ->createNamedBuilder('form1', MailFormType::class)
    ->getForm();

$form2 = $this->get('form.factory')
    ->createNamedBuilder('form2', MailFormType::class)
    ->getForm();

if ($request->request->has('form1') {
     // handle form1
}

if ($request->request->has('form2') {
     // handle form2
}
2 of 2
0

Indeed as Mihai Aurelian said you need to name the two forms in order to handle them correctly. As you can see from the posted html code, currently you have two forms containing inputs with the exactly same names. That's why it seems that both forms are submitted with exactly the same data. Since you are building your forms inside a controller and not in a separate Form Type class you should have something like this:

use Symfony\Component\Form\Extension\Core\Type\FormType;

public function yourAction(Request $request, FormFactoryInterface $formFactory) {

     $defaultData = array('message' => 'Mail');

     $form2 = $formFactory->createNamedBuilder('form2', FormType::class, $defaultData)
         ->add('content', TextareaType::class)
         ->getForm();

     $defaultData2 = array('message2' => 'Mail2');

     $form3 = $formFactory->createNamedBuilder('form3', FormType::class, $defaultData2)
         ->add('content', TextareaType::class)
         ->getForm();

     $form2->handleRequest($request);

     if ($form2->isSubmitted() && $form2->isValid()) {
          // handle form2
     }

     $form3->handleRequest($request);

     if ($form3->isSubmitted() && $form3->isValid()) {
          // handle form3
     }
}

EDIT: I fixed the missing $formFactory service from the action. But you should get FormFactory service through autowiring by type-hinting $formFactory argument in your action, instead of getting it directly from service container using $this->get('form.factory'). You should avoid getting services inside a controller this way, prefer autowiring whenever possible.

🌐
Reddit
reddit.com › r/phphelp › need help with multiple forms on one page, here
r/PHPhelp on Reddit: Need help with multiple forms on one page, here
April 5, 2023 -

https://pastebin.com/qxigq13B

Apologies if my wording seems kind of vague here, but here goes:

For each row of mySql data that is returned, I want to make the row data its own form, include either a select menu (or a number input) and push them (both row data and input value) into an array on another file. The issue is, if I use $_GET or $_POST, the row items won't come through, and if I were to use $_SESSION, it would only take row data from the last form without including the value of the number/select input. Included is a relevant code snippet, with the number or select fields omitted.

🌐
SitePoint
sitepoint.com › php
Multiple Forms on One Page - PHP - SitePoint Forums | Web Development & Design Community
July 19, 2014 - Under each Article on my website, members can post Comments. And beneath each Comment, other members can give the Comment a rating. So if there are 30 Comments, I would have 30 Forms!! What is the proper way to set up my Forms and Submit buttons so they work properly? Here is an example…
🌐
Student Tutorial
studentstutorial.com › html › html-two-forms-in-same-page.php
How to place two forms on the same page html
<!DOCTYPE html> <html> <body> <form action="login.php" method="post"> <input type="text" name="user_name"> <input type="password" name="password"> <input type="submit" value="Login"> </form> <br /> <form action="register.php" method="post"> <input type="text" name="user_name"> <input type="email" name="email"> <input type="password" name="password"> <input type="submit" value="Register"> </form> </body> </html>
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 33421053 › have-two-forms-in-one-page-and-set-session
php - Have two forms in one page and set session - Stack Overflow
<form name="name1" action="form2.php" method="post"> <input name='v1' value='1' type="submit" /> </form> <form name="name2" action="form2.php" method="post"> <input name='v2' value='2' type="submit" /> </form> ... <?php session_start(); ...
🌐
Stack Overflow
stackoverflow.com › questions › 10936359 › more-than-one-form-on-a-single-page
php - More than one form on a single page - Stack Overflow
You cannot nest forms. ... Just as an aside, you can definitely write cleaner code, and it will make your life much easier. You don't need to be echoing html wholesale: just close your php and write it, so it's readable to you, and then open ...
🌐
SitePoint
sitepoint.com › php
Can I have two forms on one page? - PHP - SitePoint Forums | Web Development & Design Community
October 8, 2014 - Hi Guys, I seem to be in a spot of bother. I have two forms on one page and think the reason they are not working is because they are colliding with each other. I am creating an online quoting system. On my estimate.php page it lists the items a customer would like. form one with a submit button “update”. Customers can change quantities of their items and the update button updates these, so the form submits to itself. update form echo '
Top answer
1 of 3
6

The action represent the page that will receive the posted data. You may use differents actions or the same action with different parameters. If you use the same action, you had to insert a parameter that permit to manage different cases. You may insert an hidden field to do this.

Consider these simple forms:

<form name="form_a" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="hidden" name="form" value="A">
    <button type="submit">Form A</button>
</form>

<form name="form_b" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="hidden" name="form" value="B">
    <button type="submit">Form B</button>
</form>

To manage the different submission, you had to check the value of the hidden field:

if(isset($_POST['form'])){

    switch ($_POST['form']) {
        case "A":
            echo "submitted A";
            break;

        case "B":
            echo "submitted B";
            break;

        default:
            echo "What are you doing?";
    } 
} 

You can't submit two separate forms at the same time, because each submission represent a different request to the server.

You may merge manually the fields of the two forms, or use Javascript to do this for you. Keep in mind that if you do this via Javascript, the field of the forms had to have differents names.

As you caan see here you can do simply via jQuery:

var str1 = $("form_a").serialize();
var str2 = $("form_b").serialize();
$.post(url, str1 + "&" + str2);

Where url is the action params of the form

2 of 3
0

Your form should be like this.
First form

 <form method="post" >
      ... 
   <input type="hidden" name="form1submission" value="yes" >
    <input type="submit" name="submit" value="Submit" >
  </form>

Second form

  <form method="post" >
      ... 
   <input type="hidden" name="form2submission" value="yes" >
    <input type="submit" name="submit" value="Submit" >
  </form>

And your php for first form.

  if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form1submission'])) {
        // first form code. 
   }

And second form php code.

  if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form2submission'])) {
        // second form code. 
   }

That's it.

Top answer
1 of 2
3

There are 2 approaches I could think of

if($POST['form_name'] == 'form1')
{
   //set my session variables
}
else  //for the second form
{
  //do something else
  //then session is intact
}

I used to put an hidden field in my form to store the name of the form. Some use the name of the submit button.

The other approach is repopulate the other form which am still thinking of how to do it without using one button for the two form

<form name="form1" action="POST" action="xxx">
    <input type="text" values="<?php print($_POST['firstform_val_1']) ?>" name="val_1" />
</form>


<form name="form2" action="POST" action="xxx">
  //these hidden fields preserve the formal values and make sure they are reposted in this second form
  <input type="hidden" name="firstform_val_1" value="<?php print($_POST['firstform_val_1']) ?>" />
  <input type="hidden" name="firstform_val_2" value="<?php print($_POST['firstform_val_2']) ?>" />

  <input type="text" name="secondform_val1" />
  <input type="submit" name="form2" />
</form>

That is the idea. I just got this from upstair, not tested. In the first form, the previous value is re-populated by the values duplicated in the hidden fields of the second form

Good Luck

2 of 2
0

If the form is being generated by a php page, you can use the following script to auto-populate any fields that were previously submitted.

Short tags enabled;

<input type="text" name="firstName" value="<?=isset($_POST['firstName'])?$_POST['firstName']:'';?>" ></input>

No Short tags;

<input type='text' name="firstName" value="
<?php
    $value=isset($_POST['firstName'])?$_POST['firstName']:'';
    echo($value);
?>
"></input>
🌐
Experts Exchange
experts-exchange.com › questions › 28514198 › using-2-forms-on-one-page.html
Solved: using 2 forms on one page | Experts Exchange
September 9, 2014 - Log in or create a free account to see answer. Signing up is free and takes 30 seconds. No credit card required. ... Learning HTML: https://developer.mozilla.org/en-US/learn/html http://www.w3schools.com/html/ http://www.codecademy.com/en/tracks/web Learning PHP and HTML forms: http://www.php.net/manual/en/tutorial.forms.php Example of two forms on one page: http://iconoun.com/demo/two_forms.php
🌐
CopyProgramming
copyprogramming.com › howto › how-to-handle-multiple-php-forms-using-one-php-page
HTML Multiple Forms in PHP: A Complete Guide to Handling Multiple Forms on One Page - Html multiple form in php
November 9, 2025 - Multiple POST forms on one page process identically regardless of count. Q: How do I handle validation errors without losing user input? A: Repopulate form fields using the submitted $_POST data. Store the data in PHP variables before validation, then output those variables as field values: value="<?php echo isset($_POST['field']) ?