PHP表单验证

我是PHP新手,请温和!

我试图用错误消息/确认消息来构建一个简单的PHP表单验证。 当我提交表格时,应该检查字段是否为空并显示相应的消息。 但它一直给我一个错误,我不知道为什么:

解析错误:语法错误,第6行的process.php中出现意外的T_IF

这里是过程php代码。

<form action="process.php" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
E-mail: <input type="text" name="email"><br>
<input type="hidden" name="submitted" value="1">
<input type="submit">
</form>

<?php
//If form was submitted

if ($_POST['submitted']==1) {
$errormsg = ""; //Initialize errors
    if ($_POST[fname]){
        $fname = $_POST[fname]; //If fname was entered
    }
    else{
        $errormsg = "Please enter first name";
    }
    if ($_POST[lname]){
        $lname = $_POST[lname]; //If lname was entered
    }
    else{
        if ($errormsg){ //If there is already an error, add next error
            $errormsg = $errormsg . ", last name";
            }
        else{
             $errormsg = "Please enter last name";
        }    
    }
    if ($_POST[email]){
        $email = $_POST[email]; //If email was entered
    }
    else{
        if ($errormsg){ //If there is already an error, add next error
            $errormsg = $errormsg . " & email";
        }else{
            $errormsg = "Please enter email";
        }
    }   
}
if ($errormsg){ //If any errors display them
    echo "<div class="box red">$errormsg</div>";
}
//If all fields present
if ($fname && $lname && $email){
    //Do something
    echo "<div class="box green">Form completed!</div>";
}
?>

一些$ _POST变量缺少单引号,这可能是导致错误的原因。 但是,一般来说,我已经提出了其他代码建议。 我将您的代码重构为更具扩展性,并采用以下增强功能遵循更好的实践:

  • 表单值会记住它们以前的值,并且不会在每个帖子中被删除。
  • 删除了“已提交”字段,并替换为if (!empty($_POST)) {}以确保发布表单。
  • 将错误消息移入数组中。 这对我来说更具可维护性和可读性(想象有15个以上的字段可供测试)。
  • 添加了validate()函数以在您的验证测试中运行。
  • 删除了变量赋值($ fname = $ _POST ['fname']),因为除了可以直接访问它们的验证之外,它们没有被使用。
  • 移动主if语句中的所有测试。
  • 希望这可以帮助!

    <form action="process.php" method="post">
    First Name: <input type="text" name="fname" value="<?php echo isset($_POST['fname']) ? $_POST['fname'] : ''?>"><br>
    Last Name: <input type="text" name="lname" value="<?php echo isset($_POST['lname']) ? $_POST['lname'] : ''?>"><br>
    E-mail: <input type="text" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''?>"><br>
    <input type="submit">
    </form>
    
    <?php
    //If form was submitted
    
    if (!empty($_POST)) {
    
        $errors = array();
    
        if (empty($_POST['fname'])){
           $errors[] = 'First name must be entered.';
        }
    
        if (empty($_POST['lname'])){
            $errors[] = 'Last name must be entered.';
        }
    
        if (empty($_POST['email'])){
           $errors[] = 'Email address must be entered.';
        }
    
    
        if ($errors){ //If any errors display them
            $error_msg = implode('<br>',$errors);
            echo "<div class="box red">$error_msg</div>";
        } 
        //If all fields present
        elseif (validate()){
            //Do something
            echo "<div class="box green">Form completed and validated!</div>";
        }
    }
    
    function validate() {
        /*you can run all your validation methods here, such as check for length, regexp email verification, etc.*/
        $validated = false;
        if ($_POST['fname'] && $_POST['lname'] && $_POST['email']) { 
            $validated = true;
        }
        return $validated;
    }
    ?>
    

    你忘了在post数组上添加" $_POST[lname]变为$_POST['lname'];错误原因,将字符串传递给你的$_POST[];

    if ($_POST["fname"]){
            $fname = $_POST[fname]; //If fname was entered
        }
        else{
            $errormsg = "Please enter first name";
        }
        if ($_POST["lname"]){
            $lname = $_POST[lname]; //If lname was entered
        }
        else{
            if ($errormsg){ //If there is already an error, add next error
                $errormsg = $errormsg . ", last name";
                }
            else{
                 $errormsg = "Please enter last name";
            }    
        }
        if ($_POST["email"]){
            $email = $_POST["email"]; //If email was entered
        }
    

    对于$ _POST变量,使用$ _POST ['您的变量名称']语法

    我更正了你的代码,如下所示:

    <form action="test.php" method="post">
    First Name: <input type="text" name="fname"><br>
    Last Name: <input type="text" name="lname"><br>
    E-mail: <input type="text" name="email"><br>
    <input type="hidden" name="submitted" value="1">
    <input type="submit">
    </form>
    
    <?php
    //If form was submitted
    
    if ($_POST['submitted']==1) {
    $errormsg = ""; //Initialize errors
        if ($_POST['fname']){
            $fname = $_POST['fname']; //If fname was entered
        }
        else{
            $errormsg = "Please enter first name";
        }
        if ($_POST['lname']){
            $lname = $_POST['lname']; //If lname was entered
        }
        else{
            if ($errormsg){ //If there is already an error, add next error
                $errormsg = $errormsg . ", last name";
                }
            else{
                 $errormsg = "Please enter last name";
            }    
        }
        if ($_POST['email']){
            $email = $_POST['email']; //If email was entered
        }
        else{
            if ($errormsg){ //If there is already an error, add next error
                $errormsg = $errormsg . " & email";
            }else{
                $errormsg = "Please enter email";
            }
        }   
    }
    if ($errormsg){ //If any errors display them
        echo "<div class="box red">$errormsg</div>";
    }
    //If all fields present
    if ($fname && $lname && $email){
        //Do something
        echo "<div class="box green">Form completed!</div>";
    }
    ?>
    
    链接地址: http://www.djcxy.com/p/69815.html

    上一篇: PHP form verification

    下一篇: How to echo multiple results from database