开发者

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. 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. Do I need to use javascript to react when someone changes the month field? 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>";
                    }
                    ec开发者_运维问答ho "</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. You can use the onchange event in javascript for the month and year elements. 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. Then you use the given result to update the #daySelect contents.

Of course (like someone suggested earlier) the jQuery datepicker is a better/easier solution in this situation.


You could define the months in an associative array as such: 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. Use the value from the array to add/subtract the correct amount of days.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜