我有一个每年更新的表单,因此用户输入日期和其他信息,然后提交存储在数据库中的表单。当他们一年后回来时,他们会做同样的事情来覆盖旧记录,问题是当他们回来时,旧日期会被回显,这是不应该发生的。所有其他字段都是空白的,这应该发生。我不是要求在代码中回显日期,但它仍然是。
定义日期下拉的代码
// parameters: $fname - main name of field
// $date - actual date value from database
// $beginYear - first value in year list
// $endYear - last value in year list
// return: none
function make_date_pulldown($fname, $date, $beginYear, $endYear)
{
// read the date and break it up into $Year, $Month and $Day
// so that we can set the "SELECTED" in the option list
if ($date == ""){
// set some default values to be safe
$Year = 0;
$Month = 0;
$Day = 0;
} else {
$Year = (int) substr($date,0,4);
$Month = (int) substr($date,5,2);
$Day = (int) substr($date,8,2);
}
// need to build a table around these guys so that there won't
// be any word wrap... it's going to be a 1 row by 5 cols.
echo "<table border=0 cellspacing=0 cellpadding=0>\n";
echo "<tr>\n";
// build month list
echo " <td><font class=bluetext face=verdana,arial size=-2>\n";
echo " <center>Month<br>\n";
echo "<select name='month_$fname'>\n";
echo " <option value='00'></option>\n";
for ($i=1;$i<=12;$i++){
printf (" <option value='%02d'",$i);
if ($i == $Month) {
printf (" SELECTED");
}
printf (">%02d</option>\n",$i);
}
echo "</select>\n";
echo " </center>\n";
echo " </td>\n";
echo " <td><font class=bluetext face=verdana,arial size=-2>\n";
echo "/";
echo " </td>\n";
// build day list
echo " <td><font class=bluetext face=verdana,arial size=-2>\n";
echo " <center>Day<br>\n";
echo "<select name='day_$fname'>\n";
echo " <option value='00'></option>\n";
for ($i=1;$i<=31;$i++){
printf (" <option value='%02d'",$i);
if ($i == $Day) {
printf (" SELECTED");
}
printf (">%02d</option>\n",$i);
}
echo "</select>\n";
echo " </center>\n";
echo " </td>\n";
echo " <td><font class=bluetext face=verdana,arial size=-2>\n";
echo "/";
echo " </td>\n";
// build year list
echo " <td><font class=bluetext face=verdana,arial size=-2>\n";
echo " <center>Year<br>\n";
echo "<select name='year_$fname'>\n";
echo " <option value='0000'></option>\n";
for ($i=$beginYear;$i<=$endYear;$i++){
printf (" <option value='%d'",$i);
if ($i == $Year) {
printf (" SELECTED");
}
printf (">%d</option>\n",$i);
}
echo "</select>\n";
echo " </center>\n";
echo " </td>\n";
echo "</tr>\n";
echo "</table>\n";
}
网站上 PHP 表单的代码
<u>Date Of Submittal</u><br>
<?php
$startYear = date("Y")-1;
$endYear = date("Y")+1;
make_date_pulldown("submitdate", $submitdate, $startYear, $endYear);
?>