Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Check which form was submitted

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.

        }       
    }            
}    
like image 993
Robbert Avatar asked Feb 04 '14 15:02

Robbert


People also ask

How do I know which form is submitted?

Use isset() method in PHP to test the form is submitted successfully or not. In the code, use isset() function to check $_POST['submit'] method. Remember in place of submit define the name of submit button. After clicking on submit button this action will work as POST method.

How do you check form is submitted or not in jquery?

$("#something-%"). submit(function(e) { e. preventDefault(); $("#some-span"). html(data); });

How do you get information from a form that is submitted using the GET method?

The Correct Answer is " Request.

What is $_ POST in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.


1 Answers

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";
like image 69
mrfazolka Avatar answered Sep 19 '22 06:09

mrfazolka