我已经设置了我的代码,以便它需要表单中的所有字段,但由于某种原因,它只适用于文本、电子邮件和密码的输入类型。因此,我的问题是如何使单选按钮、选择框和复选框也成为表单中的必填字段?下面是我的代码:
<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.
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);
}
服务器端和客户端验证:
服务器端验证在服务器端处理。有些数据无法在客户端进行验证,必须在服务器端进行验证。例如:数据库中两个日期之间的日期。
客户端验证在提交表单之前由客户端处理。使用客户端验证的优点是它减少了网络流量,因为验证是在客户端机器本身中处理的。例如电子邮件、数字、日期等
如果你想要服务器端验证(在PHP中),你需要写这样的条件:
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.
}
}
阅读更多关于php表单验证的内容:
http://phpmaster.com/form-validation-with-php/如果你想要客户端验证,那么有很多选项可供选择:
查看以下文章:
http://www.jeasyui.com/tutorial/form/form3.php希望对你有帮助。
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;
}
?>
Ultimate的答案是完美的,正是您所需要的。
我将解释什么是服务器验证和本地验证。
本地验证是当你在html代码或javascript中检查输入时。是快速的,因为它是在浏览器检查。但是任何访问你的页面的人都可以用一点技术技巧来禁用这种验证。
服务器验证是当您检查php代码中的输入时。(<?php and ?>
之间的代码)。然后在服务器中检查它。因此,任何访问您的页面的人都将而不是能够禁用该验证。
无论如何,我建议两者都使用。因为本地验证很快,而服务器验证很安全。
要添加本地验证,这个链接会很好地解释它:http://www.w3schools.com/html5/att_input_required.asp
[确保你在你的代码的第一部分的doctype设置使用html5:
<!DOCTYPE html>
<html>
<head>
...blablabalblabla more html code...
然后你的HTML与验证将导致类似这样的东西:
<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>
...
这就是html5本地验证