PHP Check which form was submitted
Asked Answered
A

4

7

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.

        }       
    }            
}    
Amusing answered 4/2, 2014 at 15:2 Comment(11)
I do not see what your problem is $_POST['page_form'] should hold either 1 or 2 and that is the form that have been submittedBrownell
Use different name for submitAtaxia
@JesperBunnyJensen So you mean this code should work? But it doesn'tAmusing
@MarioJohnathan Changed 'submit' to 'submitted' but still don't work right.Amusing
So you mean, HTML decides to submit the 1st form when clicking on the second? Never run into that!?!Siam
Give the submits a different value as in here #9538418?Siam
if you changed the name to submidted to one of the forms did you update the php code to handle the isset($_POST['submitted']) for the form you changed?Palpebrate
@dollarvar Yes that's what happening!Amusing
@Amusing : Can you post your changesAtaxia
@MarioJohnathan this is now my full form handling file phpfiddle.org/main/code/bax-d4y. I require_once this in my header and then I have 2 files comments.php and contact.php which also are included in the theme (it is Wordpress) These both files have almost the same form, with the same fields.Amusing
This is my test URL, with the 2 forms in it: 639.vc/glasbestellen/producten/testAmusing
A
12

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";
Arabinose answered 24/7, 2014 at 13:11 Comment(2)
If the form is submitted via the Enter key, the submit button variable would be missing.Dumpish
A more reliable way would be to add a hidden input with a name identifying the form in at least one of the forms.Microphone
B
2

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

Brownell answered 4/2, 2014 at 15:13 Comment(0)
S
0

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>";
}
?>
Steerage answered 4/2, 2014 at 15:10 Comment(2)
But for some reason it doesn't work. And I want to use 1 code for both forms to keep it simple and don't want to copy my whole code in each case.Amusing
It does work on my server. If you don't want to do anything special depending on the form submitted, simply remove the switch.Steerage
A
-1
<!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>
Acciaccatura answered 29/12, 2016 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.