0

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


    ?>
Kacy Raye
  • 9
  • 2
  • 5

4 Answers4

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);
    }
Sujit Singh
  • 921
  • 1
  • 10
  • 20
  • oh okay, um sorry but I just started coding about a week ago so can you tell me the code to push the error message to the array as well? Thank you! – Kacy Raye Jan 05 '13 at 09:48
  • ok. First, create $required_fields array as associative array with value as field name and key as actual name. e.g. ('month'=>'Month'). Then, in "if" part, put this :- $error[$value]=$key." is required" instead of previous line. – Sujit Singh Jan 05 '13 at 10:00
  • see, i have changed answer accordingly. – Sujit Singh Jan 05 '13 at 10:06
  • This was ALMOST perfect! This is what showed up: Array ( [first_name] => 0 is required [last_name] => 1 is required [email] => 2 is required [password] => 3 is required [sex] => 4 is required [birthday] => 5 is required [terms_of_service] => 6 is required ) When I select a month, day, and year, the [birthday] requirement is not removed from the list, but everything else worked perfectly! – Kacy Raye Jan 05 '13 at 10:31
  • ok. please post your array , so that i ll guide you accordingly. Sorry for replying late. – Sujit Singh Jan 05 '13 at 10:40
  • How do I post code in the comments like the way you did? I just made this account today so I'm not very good at navigating it... And it's fine I didn't even expect anyone to help me this much. – Kacy Raye Jan 05 '13 at 10:54
  • Wait, isn't the array what I posted at the very bottom of the code in the original question? – Kacy Raye Jan 05 '13 at 10:59
  • OH! all i had to do was change birthday to month, day, year! THANK YOU!!!! All the fields are now required! :D Answer accepted. – Kacy Raye Jan 05 '13 at 11:07
1

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.

J.K.A.
  • 7,272
  • 25
  • 94
  • 163
  • I have no idea what you mean by server and client side validation, I'm pretty new at this. But thank you for the code, i'll apply it right now. However can you tell me the code I would be putting in the //form processing and //redirect parts that you mentioned. I feel like I'm asking stupid questions but I seriously just started working with code a week ago so sorry for my ignorance... – Kacy Raye Jan 05 '13 at 09:54
  • @KacyRaye : Follow the tutorial which I have posted. You will get the idea of it. – J.K.A. Jan 05 '13 at 10:00
  • @KacyRaye : `do some form processing` part means after successful validation if you want to do some processing like insert form data into the database or redirect some etc. and `redirect to the form again` means if the form is not validated correctly then you can redirect user to you registration page – J.K.A. Jan 05 '13 at 10:03
  • Thank you! Okay I should be able to figure it out from here, and thanks for the links. I'm going to read them right now. – Kacy Raye Jan 05 '13 at 10:12
  • @KacyRaye : Most Welcome...you should accept the ans if you want ;-) – J.K.A. Jan 05 '13 at 10:13
  • How do I do that? I just made an account for this site today so I don't know how to give the people that help me a thumbs up or reputation points or whatever. – Kacy Raye Jan 05 '13 at 10:36
1

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;
}
?>
Amr
  • 4,809
  • 6
  • 46
  • 60
  • Thanks for taking the time to answer. When I tried this it worked for all the fields except the month, day, and year. And also, a notice occurred when I hit the "sign up" button saying "array to string conversion." – Kacy Raye Jan 05 '13 at 10:23
0

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.

Tomás
  • 3,501
  • 3
  • 21
  • 38
  • Thank you for the explanation, it was very clear. I already had that doctype tag at the top of my code but I didn't actually know what that meant so thanks for telling me what it represented. I have one question though! You said you recommend using both local and server side validations, but did you mean to use them both at the same time or did you simply mean to use one at a time based on the situation? – Kacy Raye Jan 05 '13 at 10:43
  • you cant use at the same time. :D When the user clicks on the submit button, the local validation will start. If the local validation checks that everything is ok, then the data from the inputs should be sent to the server. And then the server will do its validation (php code) – Tomás Jan 05 '13 at 14:44