好的,我有一个表格,在这个表格中有一堆必填字段。此表单的生日部分由 3 个字段组成:月、日和年。现在我想做的是在下拉列表的顶部有“月:”、“日:”和“年:”这些词,但我不希望这些选项成为可接受的答案!当我提交表单时没有选择实际的月、日和年,当我打印错误时,它应该说需要月、日和年!我怎样才能做到这一点?使字段成为必需字段的“if 语句”位于此代码的底部:
<form action="" method="post">
<ul id="register">
<li>
Birthday:
<select name="month">
<option value="month">Month:</option>
<option value="January">January</option>
//all the other months
</select>
<select name="day">
<option value="day">Day:</option>
<option value="1">1</option>
//all the other days of the month
</select>
<select name="year">
<option value="year">Year:</option>
<option value="2013">2013</option>
//more year options
</select><br><br>
</li>
<li>
<input type="submit" name="registrationform" value="Sign up">
</li>
</ul>
</form>
<?php
if (!empty($_POST['registrationform'])) {
if(isset($_POST['registrationform'])){
$required_fields = array('month', 'day', 'year');
foreach ( $required_fields as $key=>$value) {
if (!isset($_POST[$value]) || $_POST[$value]=='') {
$errors[$value] = $key." is required";
}
}
print_r($errors);
}
}
?>