PHP检查提交了哪个表单表单、PHP

2023-09-03 09:00:19 作者:怎敌他

我在一个网站上工作,我在1页上有2个表格。我使用1个PHP脚本来检查表单。但如果我在页面上提交我的第二个表单,我的网站就会提交第一个表单。如何检查已提交的表单?

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

        }       
    }            
}    

推荐答案

php form表单提交代码,PHP之表单的提交

如何操作:

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

并检查提交的表单:

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