PHP检查提交的表单

时间:2014-02-04 15:02:09

标签: php forms submit form-submit

我正在一个网站上工作,我在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.

        }       
    }            
}    

4 个答案:

答案 0 :(得分:7)

以这种方式做什么:

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

答案 1 :(得分:0)

我不明白你的问题是什么 虽然有点复杂,但您展示的代码应该可以正常工作。

如果它是您正在寻找的更简单的变体,我宁愿这样做:

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
}

编辑:

试一试。如果它不起作用我会感到惊讶。

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

现在,如果您不想对不同表单进行特殊处理,只需执行以下操作:

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

答案 2 :(得分:0)

我刚试过这个,我没有遇到任何问题:

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

尝试使用表单1和数据aaa提交 结果:

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

尝试使用表单2和数据bbb提交 结果:

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

所以我无法看到它不起作用

答案 3 :(得分:0)

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