【问题标题】:Form not displaying errors on required fields表单未在必填字段上显示错误
【发布时间】:2016-07-07 09:05:14
【问题描述】:

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

html/php 用于索引页面(我只附上了第一个,因为这是唯一需要 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>

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

【问题讨论】:

  • 开启错误检查,但我看到了一些
  • 在哪里?刚刚运行了一个代码检查,它没有出现编码错误(右括号、大括号等)
  • @ChrisBeechey 您可以在处理$missing 的错误并返回响应或处理表单数据后分享代码吗?

标签: php html css forms


【解决方案1】:

最简单的方法是使用required。它确保在提交表单之前填写&lt;input&gt;

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

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

【讨论】:

  • 谢谢你,不过它们需要定制。我希望它是那么简单!
猜你喜欢
  • 2011-10-08
  • 1970-01-01
  • 2015-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 2021-07-28
相关资源
最近更新 更多