PHP form verification
I'm new to PHP so please be gentle!
I'm trying to build a simple PHP form validation with an error message/confirm message. When I submit the form, it's supposed to check if fields are empty and display the corresponding message. But it keeps giving me an error and I have no idea why:
Parse error: syntax error, unexpected T_IF in process.php on line 6
Here's process php code.
<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>";
}
?>
Some of your $_POST variables were missing single quotation marks, which is probably what caused the errors. However, generally speaking, there are other code suggestions which I've layed out. I restructured your code to be more scalable and follow better practice with the following enhancements:
if (!empty($_POST)) {} to make sure form was posted.  Hope this helps!
<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
    }
For the $_POST variables use syntax as $_POST['your variable name']
I corrected your code as below:
<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>";
}
?>
下一篇: PHP表单验证
