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);
?>
Try This:
try this..
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:
Then your HTML with validation will result in something like this:
And that's html5 local validation.
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:
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.