简体   繁体   English

如何使用html和php创建收集日期的表单

[英]How to create a form with html and php that collects a date

I have a form that I want the user to plug in their birthday. 我有一个表格,希望用户插入他们的生日。 I have three items. 我有三个项目。 One for the month, one for the day, and one for the year. 一个月,一天一个,一年一个。 I am using php to create the form. 我正在使用php创建表单。 My problem is that if someone selects February as the month, they can still select day 31. There is 31 days for all of the months. 我的问题是,如果有人选择2月作为月份,他们仍然可以选择第31天。所有月份都有31天。 Do I need to use javascript to react when someone changes the month field? 当有人更改“月”字段时,我需要使用JavaScript做出反应吗? How should I go about solving this problem? 我应该如何解决这个问题? Please help. 请帮忙。 Here is my code... 这是我的代码...

                    $today = time();
                    $f_today = date("M-d-Y", $today);
                    //The Month
                    $todayMonth = date("n",$today);
                    echo "<select id='monthSelect' name='dateMonth'>";
                    for ($n=1;$n<=12;$n++) {
                        echo "<option value='$n'";
                        if ($n == $todayMonth) {
                            echo " selected='selected'";
                        }
                        echo ">$monthName[$n]</option>";
                    }
                    echo "</select>";
                    //The Day
                    $todayDay = date("d",$today);
                    echo "<select id='daySelect'name='dateDay'>";
                    for ($n=1;$n<=31;$n++) {
                        echo "<option value='$n'";
                        if ($n == $todayDay) {
                            echo " selected='selected'";
                        }
                        echo ">$n</option>";
                    }
                    echo "</select>";
                    //The Year
                    $todayYear = date("Y",$today);
                    echo "<select name='dateYear'>";
                    for ($n=$todayYear-100;$n<=$todayYear;$n++) {
                        echo "<option value='$n'";
                        if($n == $todayYear) {
                            echo " selected='selected'";
                        }
                        echo ">$n</option>";
                    }
                    echo "</select><br/><br/>";

You could use php's cal_days_in_month() which will return the correct number days for a given month and year in a calendar. 您可以使用php的cal_days_in_month()来返回日历中给定月份和年份的正确天数。 You can use the onchange event in javascript for the month and year elements. 您可以将javascript中的onchange事件用于month和year元素。 So, when a user selects the month or the year, you call a php script (via AJAX) that uses cal_days_in_month() to return the correct number of days for that month and year. 因此,当用户选择月份或年份时,您调用php脚本(通过AJAX),该脚本使用cal_days_in_month()返回该月份和年份的正确天数。 Then you use the given result to update the #daySelect contents. 然后,使用给定的结果更新#daySelect内容。

Of course (like someone suggested earlier) the jQuery datepicker is a better/easier solution in this situation. 当然(如之前建议的那样),在这种情况下, jQuery datepicker是更好/更轻松的解决方案。

You could define the months in an associative array as such: array( Jan=>30, Feb=>28,...). 您可以这样在关联数组中定义月份:array(Jan => 30,Feb => 28,...)。 Link the month select box via an onChange handler to a JS function that pulls the value of the select box (Jan, Feb, etc.) and uses that as the key in the arr. 通过onChange处理程序将月份选择框链接到JS函数,该函数提取选择框的值(Jan,Feb等),并将其用作arr中的键。 Use the value from the array to add/subtract the correct amount of days. 使用数组中的值来添加/减去正确的天数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM