PHP Check which form has been submitted

I am working on a site on which I have 2 forms on 1 page. I am using 1 PHP script to validate forms. But if I submit my second form on the page, my site will submit the first form. How can I check which form is submitted?

<!--// Form 1-->
<form method="post">

<input type="text" name="test">
<input type="submit" name="submit">
<input type="hidden" name="page_form" value="1">

</form>

<!--// Form 2-->
<form method="post">

<input type="text" name="test">
<input type="submit" name="submit">
<input type="hidden" name="page_form" value="2">

</form>

PHP:

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

    $forms = array(1, 2);
    foreach($forms as $form) {

        if($_POST['page_form'] == $form) {
        // All my form validations which are for every form the same.

        }       
    }            
}    
+3
source share
4 answers

What needs to be done like this:

<!--// Form 1-->
<form method="post">

<input type="text" name="test">
<input type="submit" name="form1" value="Submit form">

</form>

<!--// Form 2-->
<form method="post">

<input type="text" name="test">
<input type="submit" name="form2" value="Submit form">

</form>

And check which form was submitted:

if(isset($_POST["form1"]))
   echo "Form 1 have been submitted";
else if(isset($_POST["form2"]))
   echo "Form 2 have been submitted";
+6
source

I do not understand what your question is.
Although a bit confusing, the code you are showing should work fine.

If this is the simpler option you are looking for, I would prefer to do it like this:

if(isset($_POST['page_form'])) 
{
    switch ($_POST['page_form'])
    {
    case "1": // form 1 specific handling
        break;
    case "2": // form 2 specific handling
        break;
    default:
        die ("something not right there");
    }

    // common handling
}

EDIT:

try it. I would be surprised if this did not work.

<?php
if(isset($_POST['page_form'])) 
{
    switch ($_POST['page_form'])
    {
    case "1": // form 1 specific handling
        echo "form 1 submitted<br>";
        break;
    case "2": // form 2 specific handling
        echo "form 2 submitted<br>";
       break;
    default:
        die ("something not right there");
    }

    // common handling
    foreach ($_POST as $var => $val) echo "$var => $val<br>";
}
?>

<form method="post">
<input type="text" name="test">
<input type="submit" name="submit">
<input type="hidden" name="page_form" value="1">
</form>

<form method="post">
<input type="text" name="test">
<input type="submit" name="submit">
<input type="hidden" name="page_form" value="2">
</form>

, , :

<?php
if(isset($_POST['page_form'])) 
{   
    // common handling
    foreach ($_POST as $var => $val) echo "$var => $val<br>";
}
?>
0

, , :

index.html

<!--// Form 1-->
<form method="post" action="submit.php">

<input type="text" name="test">
<input type="submit" name="submit">
<input type="hidden" name="page_form" value="1">

</form>

<!--// Form 2-->
<form method="post" action="submit.php">

<input type="text" name="test">
<input type="submit" name="submit">
<input type="hidden" name="page_form" value="2">

</form>

submit.php

<?php
print_r($_POST);

1 aaa :

Array ( [test] => aaa [submit] => Submit [page_form] => 1 )

try sending with form 2 and bbb data Result:

Array ( [test] => bbb [submit] => Submit [page_form] => 2 )

So I do not see what it is, it does not work

0
source
<!DOCTYPE html>
<?php if($_SERVER['REQUEST_METHOD'] == 'POST'){
    print_r($_POST);
} ?>
<html>
    <head>
        <meta charset="UTF-8">
        <title>TRY</title>
    </head>
    <body>
        <form action="" method="post">
            <input type="text" name="test1" value='test1'>
            <input type="submit" value='Save 1'>
        </form><hr>
        <form action="" method="post">
            <input type="text" name="test2" value='test2'>
            <input type="submit" value='Save 2'>
        </form>
    </body>
</html>
0
source

All Articles