PHP表单验证无法正常工作

时间:2016-03-15 20:08:54

标签: php forms validation

我是PHP新手,正在做一个晚间课程。我们有一个表单验证项目。我有以下代码,但是当我点击提交时,它只是重定向到所需的网站,而不进行必要的验证。

<?php  
$checkedMale    = $_POST['gender'] == 'Male' ? "checked='checked'" : '';
$checkedFemale  = $_POST['gender'] != 'Male' ? "checked='checked'" : '';
$formValidates = false;
if(isset($_POST['submit']) && $_POST['submit'] == 'Register') {
$errors = array();
if ( $_POST['firstName'] == '') {
    $errors[] = '<p>Please fill in your first name</p>';   
}   
if ( $_POST['surname'] == '') {     
    $errors[] = '<p>Please fill in your surname</p>';                  
}    
if ( $_POST['email'] == '') {       
    $errors[] = '<p>Please fill in your e-mail address</p>';        
       } else {     
              if ( ! filter_var ($_POST['email'],FILTER_VALIDATE_EMAIL) ) {
                      $errors[] = "<p>Please supply a valid e-mail address</p>";
                  }
               }    
if ( $_POST['address'] == '') {     
    $errors[] = '<p>Please fill in your postal address</p>';                
}  
if (count($errors)== 0) {
    $formValidates = true;
}
}
if ( ! $formValidates) {
// Displays errors 
if (count($errors) > 0 ) {
        echo "\n<ul>";
    foreach ($errors as $error){
        echo "\n\t<li>$error</li>";
    }
    echo "\n<ul>";
}
?>
<form name="questions" action="lesson8_1.php" method="post">
    <table>
        <tr>
            <th>Title:</th>
            <td>
                <select name="title">
                    <option value="">Select</option>
                    <option>Mr</option>
                    <option>Mrs</option>
                    <option>Miss</option>
                    <option>Dr</option>                    
                </select>
            </td>
        </tr>
        <tr>
            <th>First name:</th>
            <td><input type="text" name="firstName" placeholder="First Name..." value="" /></td>
        </tr>
        <tr>
            <th>Surname:</th>
            <td><input type="text" name="surname" placeholder="Surname..." value="" /></td>                                
        </tr>
        <tr>
            <th>Email:</th>
            <td><input type="text" name="email" placeholder="E-mail Address..." value="" /></td>
        </tr>
        <tr>
            <th>Address:</th>
            <td><textarea name="address" placeholder="Postal Address..."></textarea></td>
        </tr>
        <tr>
            <th>Gender:</th>
            <td>
                <input type="radio" name="gender" value="Male"      <?php echo $checkedMale?> >Male<br>
                <input type="radio" name="gender" value="Female"    <?php echo $checkedFemale?> >Female<br>
            </td>
        </tr>
        <tr>
            <th></th>
            <td>                        
                <input type='checkbox' name='option[]' value='Car' 
                <?php echo in_array('Car',  $_POST['option']) ? 'checked' : '' ?>>I have a Car licence<br>
                <input type='checkbox' name='option[]' value='Motorcycle' 
                <?php echo in_array('Motorcycle',   $_POST['option']) ? 'checked' : '' ?>>I have a Motorcycle licence<br>
                <input type='checkbox' name='option[]' value='Fishing' 
                <?php echo in_array('Fishing',  $_POST['option']) ? 'checked' : '' ?>>I have a Fishing licence<br>
                <input type='checkbox' name='option[]' value='TV' 
                <?php echo in_array('TV',   $_POST['option']) ? 'checked' : '' ?>>I have a TV licence<br>
                <input type='checkbox' name='option[]' value='Dog' 
                <?php echo in_array('Dog',  $_POST['option']) ? 'checked' : '' ?>>I have a Dog licence<br>
            </td>
        </tr>
        <tr>
            <th></th>
            <td><input type="submit" name="submit" value="Register" /></td>
        </tr>
    </table>
</form> 
<?php } else { ?>
<h1>Your form has been successfully submitted!</h1>
<?php } ?>  

1 个答案:

答案 0 :(得分:1)

如果你的php文件被调用:&#39; lesson8_1.php&#39;,表单工作正常。有一些未确定的变量,但是当你提交它时,它会返回所需的验证(我在本地进行测试)。

  • 请填写您的名字

  • 请填写您的姓氏

  • 请填写您的电子邮件地址

  • 请填写您的邮寄地址

    标题:......

更新

如果缺少任何字段,您的代码不会存储已插入的值,您可以添加:

<input .... value="<?php if(isset($_POST['firstName'])){ echo $_POST['firstName']; } ?>" />

(在所有输入中)然后,如果表单已提交但无效,则值将保留在表单中。

更新:

将此代码保存为:lesson8.php

<?php  

$checkedMale    = $_POST['gender'] == 'Male' ? "checked='checked'" : '';
$checkedFemale  = $_POST['gender'] != 'Male' ? "checked='checked'" : '';
$formValidates = false;

if(isset($_POST['submit']) && $_POST['submit'] == 'Register') {
    $errors = array();
    if (!isset($_POST['firstName']) || $_POST['firstName'] == '') {
        $errors[] = '<p>Please fill in your first name</p>';   
    }   
    if (!isset($_POST['surname']) ||  $_POST['surname'] == '') {     
        $errors[] = '<p>Please fill in your surname</p>';                  
    }    
    if (!isset($_POST['email']) ||  $_POST['email'] == '') {       
        $errors[] = '<p>Please fill in your e-mail address</p>';        
    } else {     
        if ( ! filter_var ($_POST['email'],FILTER_VALIDATE_EMAIL) ) {
          $errors[] = "<p>Please supply a valid e-mail address</p>";
        }
    }    
    if (!isset($_POST['address']) || $_POST['address'] == '') {     
        $errors[] = '<p>Please fill in your postal address</p>';                
    }  
    if (count($errors)== 0) {
        $formValidates = true;
    }
}

if ( ! $formValidates) {
    // Displays errors 
    if (count($errors) > 0 ) {
            echo "\n<ul>";
        foreach ($errors as $error){
            echo "\n\t<li>$error</li>";
        }
        echo "\n<ul>";
    }
?>
    <form name="questions" action="lesson8.php" method="post">
        <table>
            <tr>
                <th>Title:</th>
                <td>
                    <select name="title">
                        <option value="">Select</option>
                        <option>Mr</option>
                        <option>Mrs</option>
                        <option>Miss</option>
                        <option>Dr</option>                    
                    </select>
                </td>
            </tr>
            <tr>
                <th>First name:</th>
                <td><input type="text" name="firstName" placeholder="First Name..." value="<?php if(isset($_POST['firstName'])){ echo $_POST['firstName']; }?>" /></td>
            </tr>
            <tr>
                <th>Surname:</th>
                <td><input type="text" name="surname" placeholder="Surname..." value="<?php if(isset($_POST['surname'])){ echo $_POST['surname']; }?>" /></td>                                
            </tr>
            <tr>
                <th>Email:</th>
                <td><input type="text" name="email" placeholder="E-mail Address..." value="<?php if(isset($_POST['email'])){ echo $_POST['email']; }?>" />" /></td>
            </tr>
            <tr>
                <th>Address:</th>
                <td><textarea name="address" placeholder="Postal Address..."></textarea></td>
            </tr>
            <tr>
                <th>Gender:</th>
                <td>
                    <input type="radio" name="gender" value="Male"      <?php echo $checkedMale?> >Male<br>
                    <input type="radio" name="gender" value="Female"    <?php echo $checkedFemale?> >Female<br>
                </td>
            </tr>
            <tr>
                <th></th>
                <td>                        
                    <input type='checkbox' name='option[]' value='Car' 
                    <?php echo in_array('Car',  $_POST['option']) ? 'checked' : '' ?>>I have a Car licence<br>
                    <input type='checkbox' name='option[]' value='Motorcycle' 
                    <?php echo in_array('Motorcycle',   $_POST['option']) ? 'checked' : '' ?>>I have a Motorcycle licence<br>
                    <input type='checkbox' name='option[]' value='Fishing' 
                    <?php echo in_array('Fishing',  $_POST['option']) ? 'checked' : '' ?>>I have a Fishing licence<br>
                    <input type='checkbox' name='option[]' value='TV' 
                    <?php echo in_array('TV',   $_POST['option']) ? 'checked' : '' ?>>I have a TV licence<br>
                    <input type='checkbox' name='option[]' value='Dog' 
                    <?php echo in_array('Dog',  $_POST['option']) ? 'checked' : '' ?>>I have a Dog licence<br>
                </td>
            </tr>
            <tr>
                <th></th>
                <td><input type="submit" name="submit" value="Register" /></td>
            </tr>
        </table>
    </form> 
<?php } else { ?>
    <h1>Your form has been successfully submitted!</h1>
    <!-- here you can redirect with php to the desired location -->
<?php } ?>  

它可以按照你的想法运作。