0

这是我的页面,当我运行它时,我得到了 excption,它说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>

任何帮助都将不胜感激

4

6 回答 6

4

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

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

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

希望能帮助到你!

于 2013-07-14T17:27:01.390 回答
1

您可以检查它的 $_POST 请求是否$_SERVER['REQUEST_METHOD'] === '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 ?>您想要特定于错误的消息的位置。就像在名称输入旁边一样。

于 2013-07-14T17:27:31.463 回答
1

我建议使用 .

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

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

于 2013-07-14T17:27:34.740 回答
0

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

改变

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

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

也可以看看:empty()

于 2013-07-14T17:27:35.347 回答
0

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

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

我认为它也适用于:

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

但更好用isset()

于 2013-07-14T17:27:38.360 回答
0

与其isset()到处使用,我更喜欢声明默认值。例如...

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

$form = $_POST + $defaults;

if ($form['formSubmit'] == 'Submit') {
    // ...
于 2013-07-14T17:30:37.397 回答