我有一個基本腳本,可以生成年齡驗證的下拉列表。我想要做的是在開始時爲每個下拉菜單添加選項。因此,對於一天的第一個選項是「日」,每月 - 「月」,一年 - 「年」與每一個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