我有以下输入代码,我希望用户在他的出生年份键入.我的讲师要求我使用文本框让用户将出生年份,日期和月份作为下拉列表.他还要求我们使用checkdate功能来检查用户是否正确输入了出生日期. (我正在使用Eclipse PDT)
这是php页面中输入出生日期的代码:
<p>
<b>Date of Birth:</b>
<select size="1" name="day">
<option value="" selected>Select Date</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select size="1" name="month">
<option value="" selected>Select Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<b>Year:</b>
<input type="text" name="year" size="4" maxlength="4" value="" />
所以当我使用checkdate函数时,
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
if(checkdate($month,$day,$year) == TRUE){
//$dob is a valid date
echo "<p>Date of birth:$day $month $year</p>";
}
else {
//$dob is a not valid date
$day = null;
$month = null;
$year = null;
echo "<p><b><font color=red>Please enter a <mark>valid</mark> date of birth!</font></b></p>";
显示的错误:
Warning: checkdate () expects parameter 3 to be long,string given in C:\xamp\htdocs\1300532g\Assignment_1\registerProcess.php on line 54.
题:
如何使年份文本框仅包含数字而没有负值?
我试图使用其他输入类型属性,但它也显示错误:未定义的属性值(数字)
<input type="number" name="year" size="4" maxlength="4" value="" />
解决方法:
type属性实际上与PHP处理值的方式无关.尝试将casting作为int:
$month = $_POST['month'];
$day = $_POST['day'];
$year = (int) $_POST['year'];
if(checkdate($month,$day,$year))
{
//$dob is a valid date
echo "<p>Date of birth:$day $month $year</p>";
}
else
{
//$dob is a not valid date
$day = null;
$month = null;
$year = null;
echo "<p><b><font color=red>Please enter a <mark>valid</mark> date of birth!</font></b></p>";
}