【问题标题】:How do I make radio buttons, select boxes, and checkboxes required fields in a form?如何在表单中制作单选按钮、选择框和复选框必填字段?
【发布时间】:2012-12-19 16:10:27
【问题描述】:

我已经设置了我的代码,以便它需要表单中的所有字段,但由于某种原因,它只适用于文本、电子邮件和密码的输入类型。所以我的问题是如何让单选按钮、选择框和复选框成为表单中的必填字段? 这是我的代码:

<form action="" method="post">  
     <ul id="register">
        <li>
          <input type="text" name="first_name" placeholder="First Name">
        </li>

        <li>
        <input type="text" name="last_name" placeholder="Last Name">
        </li>

       <li>
        <input type="email" name="email" placeholder="Email"><br><br>
        </li>

        <li>
      <input type="password" name="password" placeholder="Password">
        </li>

        <li>
        <input type="radio" name="sex" value="male">Male
        <input type="radio" name="sex" value="female">Female
        </li>
        <li>

        Birthday:

            <select name="month">
                    <option value="January">January</option>
                    //all the other month options
                </select>

                <select name="day">
                    <option value="1">1</option>
                    //all the other days of the month
                </select>

                <select name="year">
                    <option value="2013">2013</option>
                    //ton of year options here
                </select><br><br>
            </li>
            <li>
                <input type="checkbox" name="terms_of_service" value="termsofservice">Terms of Service<br><br>
            </li>
            <li>
                <input type="submit" name="registrationform" value="Sign up">
            </li>
        </ul>
    </form>

    <?php


    if (empty($_POST) === false) {
        $required_fields = array('first_name', 'last_name', 'email', 'password', 'sex', 'birthday', 'terms_of_service');
        foreach ($_POST as $key=>$value) {
            if (empty($value) && in_array($key, $required_fields) === true) {
                $errors[] = 'You didn\'t fill in all of the categories.';
                break 1;
            }
        }
    }
    print_r($errors);


    ?>

【问题讨论】:

  • 向我们展示使字段成为必填项的代码。
  • 不是在最底层吗?

标签: php input checkbox radio-button required-field


【解决方案1】:

Ultimate 的答案是完美的,是您所需要的。

我将向您解释什么是服务器和本地验证。

本地验证是在您检查 html 代码或使用 javascript 中的输入时。速度很快,因为它在 浏览器 中进行了检查。但是任何访问您页面的人都可以通过很少的技术技能禁用该验证。

服务器验证是在您检查 php 代码中的输入时进行的。 (&lt;?php and ?&gt; 之间的代码)。然后在 服务器 中对其进行检查。因此,访问您页面的任何人都将能够禁用该验证。

无论如何,我建议同时使用两者。因为本地验证很快,而服务器验证很安全。

要添加本地验证,这个链接会很好地解释它: http://www.w3schools.com/html5/att_input_required.asp

[确保您在代码的第一部分中将 doctype 设置为使用 html5:

<!DOCTYPE html>
<html>
<head>
...blablabalblabla more html code...

然后,您的带有验证的 HTML 将产生如下结果:

<form action="" method="post">  
 <ul id="register">
    <li>
      <input type="text" name="first_name" placeholder="First Name" required="required">
    </li>

    <li>
    <input type="text" name="last_name" placeholder="Last Name" required="required">
    </li>

   <li>
    <input type="email" name="email" placeholder="Email" required="required"><br><br>
    </li>

    <li>
  <input type="password" name="password" placeholder="Password" required="required">
    </li>

    <li>
    <input type="radio" name="sex" value="male" required="required">Male
    <input type="radio" name="sex" value="female" required="required">Female
    </li>
    <li>
...

这就是 html5 本地验证。

【讨论】:

  • 谢谢你的解释,很清楚。我已经在我的代码顶部有那个 doctype 标签,但我实际上并不知道这意味着什么,所以感谢你告诉我它代表什么。不过我有一个问题!您说您建议同时使用本地验证和服务器端验证,但您的意思是同时使用它们,还是只是根据情况一次使用一个?
  • 不能同时使用。 :D 当用户点击提交按钮时,本地验证将开始。如果本地验证检查一切正常,则应将来自输入的数据发送到服务器。然后服务器将进行验证(php代码)
【解决方案2】:

试试这个:

<form action="" method="post">  
    <ul id="register">
        <li><input type="text" name="first_name" placeholder="First Name"></li>
        <li><input type="text" name="last_name" placeholder="Last Name"></li>
        <li><input type="email" name="email" placeholder="Email"><br><br></li>
        <li><input type="password" name="password" placeholder="Password"></li>
        <li>
            <input type="radio" name="sex" value="male">Male
            <input type="radio" name="sex" value="female">Female
        </li>
        <li>
            Birthday:
            <select name="month">
                <option value="">Choose</option>
                <option value="January">January</option>
                <option value="February">February</option>
            </select>

            <select name="day">
                <option value="">Choose</option>
                <option value="1">1</option>
                <option value="2">2</option>
            </select>

            <select name="year">
                <option value="">Choose</option>
                <option value="2012">2012</option>
                <option value="2013">2013</option>
            </select><br><br>
        </li>
        <li><input type="checkbox" name="terms_of_service" value="termsofservice">Terms of Service<br><br></li>
        <li><input type="submit" name="registrationform" value="Sign up"></li>
    </ul>
</form>

<?php
if (!empty($_POST)) {
    $required_fields = array('first_name', 'last_name', 'email', 'password', 'sex', 'month', 'day', 'year', 'terms_of_service');
    foreach ($required_fields as $value) {
        if (empty($_POST["$value"])) {
            $errors .= "$value is required<br>";
        }
    }            
    echo $errors;
}
?>

【讨论】:

  • 感谢您抽出宝贵时间回答。当我尝试这个时,它适用于除月、日和年之外的所有字段。而且,当我点击“注册”按钮说“数组到字符串转换”时,会出现一个通知。
【解决方案3】:

试试这个..

 if(isset($_POST['registrationform'])){
       $required_fields = array( /* all required fields including radio, select and 
checkboxes  as associative array with key as actual name and value as field name */);
       foreach ( $required_fields as $key=>$value) {
             if (!isset($_POST[$value]) || $_POST[$value]=='') {
                  $errors[$value] = $key." is required";

                }
       }

       print_r($errors);
    }

【讨论】:

  • 哦,好吧,对不起,我大约一周前才开始编码,所以你能告诉我将错误消息也推送到数组的代码吗?谢谢!
  • 好的。首先,创建 $required_fields 数组作为关联数组,其中值作为字段名称,键作为实际名称。例如('月'=>'月')。然后,在 "if" 部分中,输入:- $error[$value]=$key." is required" 而不是上一行。
  • 看,我已经相应地改变了答案。
  • 这几乎是完美的!这就是显示的内容: Array ( [first_name] => 0 是必需的 [last_name] => 1 是必需的 [email] => 2 是必需的 [password] => 3 是必需的 [sex] => 4 是必需的 [生日] => 5 是必需的 [terms_of_service] => 6 是必需的)当我选择月、日和年时,[生日] 要求并没有从列表中删除,但其他一切都很好!
  • 好的。请张贴您的数组,以便我相应地指导您。抱歉回复晚了。
【解决方案4】:

服务器端和客户端验证:

服务器端验证在服务器中处理。某些数据无法在客户端进行验证,必须在服务器端进行验证。例如数据库中两个日期之间的日期。

客户端验证在提交表单之前在客户端进行处理。使用客户端验证的优点是它减少了网络流量,因为验证是在客户端机器本身中处理的。例如电子邮件不是数字 isdate 等。

如果您想要服务器端验证(在 PHP 中),您需要编写如下条件:

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $error_msg = array();
    if(!isset($_POST['your_radio_button_name'])){
        $error_msg[] = "Enter the required fields";
    }
    if(!isset($_POST['your_checkbox_button_name'])){
        $error_msg[] = "Enter the required fields";
    }
    if(!isset($_POST['your_select_box_name'])){
        $error_msg[] = "Enter the required fields";
    }

    if(isset($error_msg) && count($error_msg) == 0){
        // do some form processing
    }
    else{
        // redirect to the form again.
    }
} 

阅读更多关于 php 中的表单验证:

http://phpmaster.com/form-validation-with-php/

如果你想要客户端验证,那么有很多可用的选项:

查看以下文章:

http://www.jeasyui.com/tutorial/form/form3.php

希望对你有帮助。

【讨论】:

  • 我不知道服务器和客户端验证是什么意思,我对此很陌生。但是谢谢你的代码,我现在就应用它。但是,您能告诉我我将在您提到的//表单处理和//重定向部分中添加的代码吗?我觉得我在问愚蠢的问题,但我一周前才真正开始使用代码,很抱歉我的无知......
  • @KacyRaye :按照我发布的教程进行操作。你会明白的。
  • @KacyRaye : do some form processing 部分表示成功验证后,如果您想进行一些处理,例如将表单数据插入数据库或重定向一些等,redirect to the form again 表示表单未正确验证然后您可以将用户重定向到您的注册页面
  • 谢谢!好的,我应该能够从这里弄清楚,并感谢您的链接。我现在就去读。
  • @KacyRaye : 最欢迎...如果你愿意,你应该接受 ans ;-)
猜你喜欢
  • 2015-01-29
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
相关资源
最近更新 更多