使用 HTML/PHP 的生日表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6961007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:41:07  来源:igfitidea点击:

Birthday form using HTML/PHP

phphtmlforms

提问by patokun

I'm making a registration form which will have the user put in his birthday. The problem is when it comes to February and months who do not end in 31. The user is able to put something like 31 February or 31 April. Is there any practical way to display just 28 when February is selected? (aside from using Ajax)

我正在制作一个注册表,让用户输入他的生日。问题是当涉及到 2 月和不以 31 结束的月份时。用户可以输入类似 31 月或 4 月 31 日的内容。2月选择28岁时是否有任何实用的方法?(除了使用 Ajax)

<html>
<body>
<form action="registration.php" method="post">

<p><u>Select table</u></p>

<p>Select date:</p>
<select name="day">
<?php
for($i=1;$i<=31;$i++)
{
    echo '<option value='.$i.'>'.$i.'</option>';
}


<select name="month">
<option value="January">January</option>
<option value="February">February</option>
<option value="Mars">Mars</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>

<select name="year">
<?php
for($i=2011;$i<=2015;$i++)
{
    echo '<option value='.$i.'>'.$i.'</option>';
}

?>
</select>
<br/><br/>
<input type="submit" value="Submit" />
</form>

</body>
</html>

采纳答案by Justin Ethier

You could use JavaScript to restrict the dropdown to 28, 30, or 31 days based on the month. This does not require you to use AJAX.

您可以使用 JavaScript 根据月份将下拉列表限制为 28、30 或 31 天。这不需要您使用 AJAX。

回答by Williham Totland

Don't do that.

不要那样做。

Most people are most comfortable typing their birthday out in full. The <option>/<select>bit is reallyunnecessary.

大多数人最喜欢完整地输入他们的生日。这个<option>/<select>真的没有必要。

If you don't have access to a good date guesser server side (odds are you do), break it into three separate fields, but usually, that's not even necessary.

如果您无法访问良好的日期猜测器服务器端(您这样做的可能性很大),请将其分成三个单独的字段,但通常,这甚至没有必要。

Just give them a single field that can comfortably fit 10 digits, and if the date is ambiguous when you receive it, ask the user if you guessed right.

只需给他们一个可以轻松容纳 10 位数字的字段,如果收到时日期不明确,请询问用户是否猜对。

回答by Lotus Notes

http://jqueryui.com/demos/datepicker/

http://jqueryui.com/demos/datepicker/

Use this and all your date problems are solved forever.

使用它,您所有的日期问题都将永远解决。

回答by user2816686

<html>
<head>
<title>Test</title>
</head>
<body>
Date Of Birth:
<select name="month" onChange="changeDate(this.options[selectedIndex].value);">
<option value="na">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="day" id="day">
<option value="na">Day</option>
</select>
<select name="year" id="year">
<option value="na">Year</option>
</select>
<script language="JavaScript" type="text/javascript">
function changeDate(i){
var e = document.getElementById('day');
while(e.length>0)
e.remove(e.length-1);
var j=-1;
if(i=="na")
k=0;
else if(i==2)
k=28;
else if(i==4||i==6||i==9||i==11)
k=30;
else
k=31;
while(j++<k){
var s=document.createElement('option');
var e=document.getElementById('day');
if(j==0){
s.text="Day";
s.value="na";
try{
e.add(s,null);}
catch(ex){
e.add(s);}}
else{
s.text=j;
s.value=j;
try{
e.add(s,null);}
catch(ex){
e.add(s);}}}}
y = 1993;
while (y-->1940){
var s = document.createElement('option');
var e = document.getElementById('year');
s.text=y;
s.value=y;
try{
e.add(s,null);}
catch(ex){
e.add(s);}}
</script>
</body>
</html>