我有一个基本脚本,可以生成年龄验证的下拉列表。我想要做的是在开始时为每个下拉菜单添加选项。因此,对于一天的第一个选项是“日”,每月 - “月”,一年 - “年”与每一个0值下面的代码:PHP年龄验证下拉列表
<?php
//define the year
$thisYear = date('Y');
$filtered = array_map("strip", $_POST);
function strip($val){
$val = strip_tags($val);
$val = htmlentities($val,ENT_QUOTES);
return $val;
}
function renderDropDown($name, $opts, $valueIsKey=true){
$out = array();
$out[] = '<select name="'.$name.'">';
foreach($opts as $key => $val){
if($valueIsKey){
$out[] = '<option value="'.$key.'">'.$val.'</option>';
} else {
$out[] = '<option value="'.$val.'">'.$val.'</option>';
}
}
$out[] = '</select>';
return implode("\n", $out);
}
if($_POST['submit'] != 'submit' && !isset($_POST['submit'])){
//define text months
for($i=2; $i<=13; $i++){
$calTime = mktime(0, 0, 0, $i, 0, $thisYear);
$months[date('m', $calTime)] = date('M', $calTime);
}
$renderHTML = true;
} else {
//try to construct a valid date from post data
if(checkdate($filtered['months'], $filtered['days'], $filtered['years'])){
//valid date..check if they are 18+
$validAge = $thisYear - 18;
if($filtered['years'] <= $validAge){
//inside you go
die('yes');
} else {
header('Location: http://www.google.com');
}
} else {
//invalid date.. try again mr hacker
}
}
if($renderHTML){
?>
<form name="ageVerifier" action="" method="post">
Day: <?php print(renderDropDown('days', range(1,31), false)); ?>
Month: <?php print(renderDropDown('months', $months)); ?>
Year: <?php print(renderDropDown('years', range($thisYear, $thisYear-100), false)); ?>
<input type="submit" name="submit" value="submit" />
</form>
<?php
}
?>
任何帮助,在此,将不胜感激。
干杯, 克里斯
什么问题在哪里? – 2011-01-21 12:19:12
可能没有帮助,但我认为日期选择下拉不好的设计。这对用户来说很不方便,带宽浪费,并给你一种错误的验证感。更好地提供文本输入并验证您正确接收的内容。 – tdammers 2011-01-21 12:31:33