Ahmed Hamdy
Ahmed Hamdy

Reputation: 97

PHP multiple forms with single action

I've multiple forms with a single action, a single php page that gets called by all the forms.

How can I differentiate which form was sent to the php page?

Upvotes: 0

Views: 1307

Answers (5)

kevinandrada
kevinandrada

Reputation: 499

On the submit button for each form, use different names. Something like:

<input type="submit" name="submit_1" value="Submit" />
<input type="submit" name="submit_2" value="Submit" />
<input type="submit" name="submit_3" value="Submit" />
...

Then on your PHP, you'll have:

$_POST["submit_1"]
$_POST["submit_2"]
$_POST["submit_3"]

Upvotes: 0

Saturnix
Saturnix

Reputation: 10564

Using a different unique input type="hidden" for each form.

HTML:

<input type="hidden" name="form_id" value="1">
<input type="hidden" name="form_id" value="2">

PHP:

$myform = $_POST["form_id"];

You can also use the submit button but note that the "value" parameter is what gets displayed to the user so you won't be able to modify it (assuming you want the same text to be displayed on every button).

<input type="submit" name="action" value="the user saw this">

PHP:

$_POST["action"] // -> "the user saw this";

Upvotes: 4

jterry
jterry

Reputation: 6269

Add a hidden field (action or the like) to each field, then check for it.

<form id="num1">
    <input type="hidden" name="action" value="first_action" />
</form>

...and the check:

<?php

    if(!empty($_REQUEST['action']) {
        switch($_REQUEST['action']) {
            case 'first_action':
                // first action code

                break;
        }
    }

?>

Upvotes: 1

Hawili
Hawili

Reputation: 1659

You can detect this from the submit button itself too, if submit has different values like:

update name, update profile, delete users...

Upvotes: 0

Paflow
Paflow

Reputation: 2347

Give each submitbutton an other name or put a with different values in each form.

Upvotes: 0

Related Questions