下拉而不是输入

时间:2014-10-22 21:34:09

标签: php html

校验!     

我的网站有这个输入字段,我怎么能用下拉代替它,所以它仍然会将数据发送到query.php文件?没有下拉列表它工作正常,但我想添加下拉列表,因为我不会在代码中包含每一年,所以它对用户来说会更容易。

PHP:

<?php
$input = $_POST["textarea"];

echo 'In year '. $input .' the population of the world was:';
echo "<br />";

if($input == 1980){
echo "1980-";
}elseif($input == 1981){
echo "You did not write a";
}elseif($input == 1982){
echo "You did not write a";
}elseif($input == 1983){
echo "You did not write a";
}elseif($input == 1984){
echo "You did not write a";
}elseif($input == 1985){
echo "You did not write a";
}elseif($input == 1986){
echo "You did not write a";
}elseif($input == 1987){
echo "You did not write a";
}elseif($input == 1988){
echo "You did not write a";
}elseif($input == 1989){
echo "You did not write a";
}elseif($input == 1990){
echo "You did not write a";
}elseif($input == 1991){
echo "You did not write a";
}elseif($input == 1992){
echo "You did not write a";
}elseif($input == 1993){
echo "You did not write a";
}elseif($input == 1994){
echo "You did not write a";
}elseif($input == 1995){
echo "You did not write a";
}elseif($input == 1996){
echo "You did not write a";
}elseif($input == 1997){
echo "You did not write a";
}elseif($input == 1998){
echo "You did not write a";
}elseif($input == 1999){
echo "You did not write a";
}elseif($input == 2000){
echo "You did not write a";
}elseif($input == 2001){
echo "You did not write a";
}elseif($input == 2002){
echo "You did not write a";
}elseif($input == 2003){
echo "You did not write a";
}elseif($input == 2004){
echo "You did not write a";
}elseif($input == 2005){
echo "You did not write a";
}elseif($input == 2006){
echo "You did not write a";
}elseif($input == 2007){
echo "You did not write a";
}elseif($input == 2008){
echo "You did not write a";
}elseif($input == 2009){
echo "You did not write a";
}elseif($input == 2010){
echo "You did not write a";
}elseif($input == 2011){
echo "You did not write a";
}elseif($input == 2012){
echo "You did not write a";
}elseif($input == 2013){
echo "You did not write a";
}elseif($input == 2014){
echo "You did not write a";
}else{
echo "Syntax error: You wrote in a wrong year!";
}


?>

HTML:

<form method="post" action="query.php"> 
<input type="text" class="form-control" id="textarea" placeholder="Year">
<button type="submit" name="inputuno" value="Submit" class="btn btn-theme">Check!</button>
</form>

1 个答案:

答案 0 :(得分:0)

替换此代码的html输入

<select id="year" name="year">
  <option value="1980" selected>1980</option>
  <option value="1981">1981</option>
</select>

继续添加选项以添加更多年份。

你会发现$ _POST [&#39; year&#39;]来包含年份。请注意,这不是验证输入的方法,因为任何人都可以在浏览器中修改您的html页面并添加一个额外的选项标记,该标记将包含他们想要放入的任何邪恶值。