php check post包含一个变量

时间:2013-07-14 17:21:56

标签: php

当我运行它时,这是我的页面,我得到的结论是formSubmit没有定义。我知道我必须检查post内容是否可变,但我不知道解决方案

 <?php
    if($_POST['formSubmit'] == "Submit")
    {
        $errorMessage = "";

        if(empty($_POST['formPassword']))
        {
            $errorMessage .= "<li> You forgot to enter your password!</li>";
        }
        if(empty($_POST['formName']))
        {
            $errorMessage .= "<li> You forgot to enter a name!</li>";
        }

        $varPassword = $_POST['formPassword'];
        $varName = $_POST['formName'];

        if(empty($errorMessage)) 
        {
            require('Document1.php');
            User::add_user($varName, $varPassword);
            exit;
            //header("Location: normal.php");
        }


    }
    ?>

    <html>
       <head>

        <title>SIGN UP</title>

        <style type="text/css">
    <!--
    .style1 {
        font-size: xx-large;
        color: #0000CC;
    }
    -->
        </style>
    </head> 

     <body>
     <p align="center"><br>
       <span class="style1">SIGN UP</span></p>
     <p>&nbsp;</p>
     <form action="myform1.php" method="post">

            <p>
                What is Your NAME ?<br>
            <input type="text" name="formName" maxlength="50" value="" />               
            </p>

            <p>
                What is Your PASSWORD ?<br>
                <input type="text" name="formPassword" maxlength="50" value="" />
            </p>

            <?php
            /*
            ?>
            <p>
                What is Your Email ?<br>
                <input type="text" name="formEmail" maxlength="50" value="" />
            </p>
            */
                ?>          
            <input type="submit" name="formSubmit" value="Submit" />

            <input type=reset value = "delete fields ">

     </form>
     </body>
    </html>

任何帮助都会被哄骗

6 个答案:

答案 0 :(得分:4)

原因是因为即使表单尚未提交,PHP代码也会在页面加载时第一次运行。解决方案是按如下方式更改您的第一个IF语句:

<?php
if(isset($_POST['formSubmit']) && $_POST['formSubmit'] == "Submit")
{
// OTHER CODE REMAINS AS IT IS
}
?>

现在它将检查不仅$ _POST ['formSubmit']设置为“提交”,而且它还存在。页面首次加载时,$ _POST ['formSubmit']将不可用,因此不会抛出任何异常。

希望它有所帮助!

答案 1 :(得分:1)

您可以检查带有$_SERVER['REQUEST_METHOD'] === 'POST'的$ _POST请求是否也会向阵列添加错误,这样您就可以将错误放在所需位置。

<?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $error = array();

    if(empty($_POST['formPassword']))
    {
        $error['pass'] = "You forgot to enter your password!";
    }
    if(empty($_POST['formName']))
    {
        $error['name'] = "You forgot to enter a name!";
    }

    if(empty($error))
    {
        require('Document1.php');
        User::add_user($_POST['formName'], $_POST['formPassword']);
        exit;
        //header("Location: normal.php");
    }
}
?>

然后,如果错误只是将<?php echo isset($error['name']) ? $error['name'] : null ?>添加到您希望消息特定于错误的位置。就像名字输入旁边一样。

答案 2 :(得分:1)

我建议使用。

if (isset($_POST['formSubmit']) && ($_POST['formSubmit'] == "Submit")) {
    /* ... */
}

isset()方法将检查值是否存在

答案 3 :(得分:0)

使用isset()检查变量是否设置始终是一个好习惯。

更改

if($_POST['formSubmit'] == "Submit")

if(isset($_POST['formSubmit']) && $_POST['formSubmit'] == "Submit"){
//rest of the code
}

另请参阅:empty()

答案 4 :(得分:0)

在您尝试获取$_POST['formSubmit']的值之前,您应该检查它是否设置了isset($_POST['formSubmit'])

if (isset($_POST['formSubmit']) && $_POST['formSubmit'] == "Submit") {
    // your code
}

我认为它也适用于:

if ($_POST['formSubmit'] && $_POST['formSubmit'] == "Submit") {
    // your code
}

但最好使用isset()

答案 5 :(得分:0)

我更喜欢声明默认值,而不是在任何地方使用isset()。例如......

$defaults = array(
    'formSubmit' => '',
    'formPassword' => '',
    'formName' => '',
);

$form = $_POST + $defaults;

if ($form['formSubmit'] == 'Submit') {
    // ...