How do I make radio buttons, select boxes, and checkboxes required fields in a form?

笑着哭i 提交于 2019-12-13 05:59:35

问题


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


    ?>

回答1:


try this..

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



回答2:


Server side and Client Side validation :

Server side validation is processed in the server. Some data cannot be validated in the client side and it has to be validated in the server side. Eg Date between the two dates in the database.

Client side validation is processed the client side before submitting the form. The advantage of using the client side validation is it reduces the network trafic since the validation is processed in the client machine itself. Eg email isnumeric isdate etc.

If you want Server side validation (in PHP) you need to write conditions like this:

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.
    }
} 

Read more about form validation in php:

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

And if you want client side validation then there are number of options available for it:

Check the following article:

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

Hope it'll help you.




回答3:


Try This:

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



回答4:


The answer from Ultimate is perfect and is what you need.

I'll explain you what is server and local validation.

Local validation is when you check the inputs in your html code or with javascript. Is fast because It is checked in the browser. But anyone visiting your page will be able to disable that validation with little technical skill.

Server validation is when you check the inputs in your php code. (the code that is between the <?php and ?>). It is checked then, in the server. So anyone visiting your page will not be able to disable that validation.

Anyway, I recommend using both. Because local validation is fast, and server validation is secure.

To add local validation, this link will explain it very well: http://www.w3schools.com/html5/att_input_required.asp

[Make sure you have in the first part of your code the doctype set to use html5:

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

Then your HTML with validation will result in something like this:

<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>
...

And that's html5 local validation.



来源:https://stackoverflow.com/questions/14170396/how-do-i-make-radio-buttons-select-boxes-and-checkboxes-required-fields-in-a-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!