0

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?

1
  • 2
    Add a hidden <input> field with some value to identify the origin? Commented Jun 20, 2013 at 12:41

5 Answers 5

4

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";
Sign up to request clarification or add additional context in comments.

2 Comments

How about changing the name of the submit button? Like: <input type="submit" name="uploadform" value="Submit"/>
you can do that as well, but you can only change the name not the value. This will make it more difficult if you want to manage it as an array. Using hidden field is better in this case.
1

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;
        }
    }

?>

Comments

0

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

Comments

0

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

update name, update profile, delete users...

Comments

0

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.