表单未在必填字段上显示错误


Form not displaying errors on required fields

我第一次尝试注册表单,一直在关注教程视频,我发誓我已经遵循了所有步骤,但是当我希望姓氏和名字字段成为必填字段时,但是当我测试它并点击提交时,表单仍然处理为空白。

用于索引页的html/php(我只附加了第一个have,因为这是唯一具有所需php的部分):

<?php
//arrays for error messages
$errors = [];
$missing = [];
if (isset($_POST['submit'])){
    $expected = ['gender','title','surname','given','day','month','year','prevsurname','prevfirst','address','city','postcode','state','australian'];
    $required = ['surname','given'];
    require './includes/process_mail.php';
}
?>
<!DOCTYPE html>
    <head>
        <title>FORM ASSIGNMENT</title>
        <link href="CSS/style.css" rel="stylesheet" type="text/css">
    </head>
<body>
    <h3 class="head">registration</h3>
    <p class="explain">Complete the form below to register.</p>
<!----- ADDED REQUIRED FIELD TEXT USING SPAN CLASS, AS WILL NOT CHANGE THE VISUALS OF THE SITE ----->
    <form method="POST" action="handle_form.php">
        <fieldset><legend>Sign up below</legend>
            <?php if ($errors || $missing) : ?>
                <p class="error">Please complete missing field(s) before progressing</p>
            <?php endif; ?>
            <table width="600" border="2">
                <tr>
                    <input type="radio" name="gender" value="male" checked> Male
                    <input type="radio" name="gender" value="female"> Female
                </tr>
                <tr>
                    <th align="right">Title</th>
                        <td><select name="title" id="title">
                            <option value="Default" selected>Select</option>
                            <option value="Mr">Mr.</option>
                            <option value="Miss">Miss.</option>
                            <option value="Ms">Ms.</option>
                            <option value="Mrs">Mrs.</option>
                        </select></td>
                </tr>
                <tr>
                    <th align="right">Name</th>
                    <td><label for="surname">Surname:
                            <?php if ($missing && in_array('surname', $missing)): ?>
                                <span class="error">Please enter Surname</span>
                            <?php endif; ?>
                        </label>
                        <input name="surname" type="text" id="surname" size="30" maxlength="25"><br>
                        <label for="given">Given:
                            <?php if ($missing && in_array('given', $missing)): ?>
                                <span class="error">Please enter First Name</span>
                            <?php endif; ?>
                        </label>
                        <input name="given" type="text" id="given" size="33" maxlength="25"></td>
                </tr>

用于处理错误的 PHP:

<?php
    foreach ($_POST as $key => $value){
        $value = is_array($value) ? $value : trim($value);
    if (empty($value) && in_array($key, $required)){
        $missing[] = $key;
        $$key = '';
    }elseif (in_array($key, $expected)){
        $$key = $value;
    }
}

PHP 显示已处理的数据:

<!DOCTYPE html>
    <head>
        <title>FORM ASSIGNMENT</title>
        <link href="CSS/style.css" rel="stylesheet" type="text/css">
    </head>
<body>
    <!-----------  PHP CODE TO DISPLAY DATA FROM FORM --------------->
     <pre>  
    <?php
        if($_GET){
        echo 'Content of the $_GET array;<br>';
            print_r($_GET);
        }elseif ($_POST){
            echo 'Details below <br>';
            print_r($_POST);
        }
    ?>
    </pre>
    <h3>Details Uploaded Successfully!</h3>
    <p class="thankyou">Thank you for filling out your details</p>
</body>
</html>

任何提示、提示或滥用都会有所帮助,谢谢。

最简单的方法是使用 required。它确保在提交表单之前填写<input>

<input name="surname" type="text" id="surname" size="30" maxlength="25" required>
<input name="given" type="text" id="given" size="33" maxlength="25" required>