Multiple HTML Forms on One Page
Asked Answered
P

5

10

If I have multiple HTML tags with separate submit forms, how do I know which when was posted in the PHP file that processes the form's data?

Pep answered 2/3, 2012 at 17:45 Comment(0)
C
8

The easiest way is to name your submit buttons unique to the form. You can also use this if you have multiple submit buttons for a SINGLE form (eg - submit and save, submit and return, submit and exit)

 <input type="submit" name="action" value="Save">
 <input type="submit" name="action" value="Return">
 <input type="submit" name="action" value="Exit">

The $_POST array (or $_GET/$_REQUEST) will contain the key "action" with the value of the enacted button (whether clicked or not).

As a rule, I avoid passing hidden text fields, etc, that are unnecessary - simply to keep the code more clean.

So. For your application, I'd give your submit button values as such:

<form id="Form1">
  <input type="submit" name="action" value="Form1">
</form>

<form id="Form2">
 <input type="submit" name="action" value="Form2">
</form>

<form id="Form3">
 <input type="submit" name="action" value="Form3">
</form>
Counterweight answered 2/3, 2012 at 18:9 Comment(0)
K
8

You can set such an input in each form:

<input type="hidden" name="form_id" value="identifier_of_form" />

For example:

<form method="post" action="">
    <input type="hidden" name="form_id" value="form_0" />
    <!-- snip -->
</form>

<form method="post" action="">
    <input type="hidden" name="form_id" value="form_1" />
    <!-- snip -->
</form>

<form method="post" action="">
    <input type="hidden" name="form_id" value="form_n" />
    <!-- snip -->
</form>
Keewatin answered 2/3, 2012 at 17:46 Comment(0)
C
8

The easiest way is to name your submit buttons unique to the form. You can also use this if you have multiple submit buttons for a SINGLE form (eg - submit and save, submit and return, submit and exit)

 <input type="submit" name="action" value="Save">
 <input type="submit" name="action" value="Return">
 <input type="submit" name="action" value="Exit">

The $_POST array (or $_GET/$_REQUEST) will contain the key "action" with the value of the enacted button (whether clicked or not).

As a rule, I avoid passing hidden text fields, etc, that are unnecessary - simply to keep the code more clean.

So. For your application, I'd give your submit button values as such:

<form id="Form1">
  <input type="submit" name="action" value="Form1">
</form>

<form id="Form2">
 <input type="submit" name="action" value="Form2">
</form>

<form id="Form3">
 <input type="submit" name="action" value="Form3">
</form>
Counterweight answered 2/3, 2012 at 18:9 Comment(0)
B
5

By including some unique data in each one. Possibly with a hidden input.

Breviary answered 2/3, 2012 at 17:46 Comment(0)
K
1

why not just set the form names as "action1", "actions2", "action3",

If so, no hidden input needed.

Kryska answered 6/12, 2013 at 1:16 Comment(0)
I
0

This works for me....

  1. Each form has a unique 'name'...

    form method="post" action="" name="uniqueName" id= "uniqueName"

  2. Each form has this hidden field...

    input type="hidden" id="action1_1" name="action1" value="1"

with unique id extension ( _1, _2, 3 ... and unique value (1,2,3,....) and common name: action1

  1. then in the php index script :

    $action1 = $_POST['action1'];

    if ( $action1 == "1" ) { include("form_process1.php"; //...do whatever that form need dones... } else if ( $action1 == "whateverelse" ) { include("form_process.php"; // ...do whatever that form does... }

Ilona answered 4/2, 2013 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.