如何在表单中创建单选按钮,选择框和复选框必填字段? [英] How do I make radio buttons, select boxes, and checkboxes required fields in a form?

查看:1452
本文介绍了如何在表单中创建单选按钮,选择框和复选框必填字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've set up my code so that it would require all the fields within the form, but for some reason it's only applying to input types of text, email, and password. So my question is how can I get the radio buttons, select boxes, and checkbox to also be required fields within the form? Here's my code:

<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);


    ?>


推荐答案

b

 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);
    }

这篇关于如何在表单中创建单选按钮,选择框和复选框必填字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆