-1

我正在使用一个非常有效的脚本来验证我正在开发的网站的年龄。目前它使用下拉菜单来选择月、日、年。我希望可以更改下拉菜单以让用户输入月、日、年。

这是代码:

                <?php

            define('MIN_AGE', 21); // Enter the minimum age that your website visitors must be to view your content (replace 18 with your number)
            define('COOKIE_DAYS', 30); //Enter the number of days you would like the cookie to last (replace 30 with your number)

            // BE CAREFUL EDITING ANYTHING BELOW THIS LINE //

            date_default_timezone_set('America/Los_Angeles');
            function get_birth_drops($year = '', $month = '', $day = '')
            {
                $out = '';

                $year = $year ? $year : '';
                $month = $month ? $month : '';
                $day = $day ? $day : '';

                $month_select = '<label>MM: <select name="bmonth" id="bmonth"><option value=""></option>';
                for($i = 1; $i <=12; $i ++)
                {
                    $selected = ($i == $month) ? ' selected':'';
                    $month_select .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
                }
                $month_select .= '</select></label>';

                $day_select = ' <label>DD: <select name="bday" id="bday"><option value=""></option>';
                for($i = 1; $i <=31; $i ++)
                {
                    $selected = ($i == $day) ? ' selected':'';
                    $day_select .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
                }
                $day_select .= '</select></label>';

                $year_select = ' <label>YYYY: <select name="byear" id="byear"><option value=""></option>';
                for($i = 2010; $i >= 1950; $i --)
                {
                    $selected = ($i == $year) ? ' selected':'';
                    $year_select .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
                }
                $year_select .= '</select></label>';

                $out = $month_select . $day_select . $year_select;

                return $out;
            }

            function calculateAge($birthday){
                return floor((time() - strtotime($birthday))/31556926);
            }
            ?>

有什么想法吗?万分感谢!

4

2 回答 2

0

目前,该函数get_birth_drops将三个字段输出为选择元素。要将它们更改为文本输入,只需更改存储选择字段 html 的变量:

例子:

$month_select = '<label>MM: <input type="number" name="bmonth" id="bmonth" /><label>';

但是,这不会验证数据,因此您可能需要包含一些 JavaScript 来处理它。也可以使用 HTML5 进行验证:

例子:

$month_select = '<label>MM: <input type="number" min="1" max="31" name="bmonth" id="bmonth" /><label>'

希望有帮助。

于 2013-09-14T00:38:19.130 回答
0

您应该改用文本输入字段:

//Month input field instead of select field
$month_select = '<label>MM: <input type="text" name="bmonth" id="bmonth" value="' . $month . '" /></label>';
于 2013-09-14T00:34:35.107 回答