PHP Check which form was submitted

form-submit, forms, php, submit

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

Problem

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. } } } ```

Original source

Related problems