PHP Check which form was submitted

143

I am working on a website where I have 2 forms on 1 page. I use 1 PHP script to check the forms. But if I submit my second form on the page, my website submits 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.

        }       
    }            
}    
401

Answer

Solution:

What to do it this way:

<!--// 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";
334

Answer

Solution:

I just tested this and i have no problems what so ever:

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

trying submit with form 1 and data aaa result:

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

trying submit with form 2 and data bbb result:

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

So i cant see what it is that is not working

270

Answer

Solution:

I don't understand what your question is.
Though a bit convoluted, the code you show should work fine.

If it's a simpler variant you're looking for, I would rather do it that way:

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's not right there");
    }

    // common handling
}

EDIT:

try this out. I would be surprised if it 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's 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>

Now if you don't want a special handling for different forms, simply do this:

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

Answer

Solution:

<!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>

People are also looking for solutions to the problem: ajax - PHP/GD: Wind direction generated image from database value

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.