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