0

虽然我意识到这不是最有效的代码,但它不会在 8 月或 9 月工作!我正在使用 PHP 5.4 和 XHTML 1.0 过渡。我已经在最新的 IE、Firefox 和 Google 浏览器中对其进行了测试。我检查了发送到浏览器的代码,8 月或 9 月没有任何不同。所有其他月份工作正常。当用户第一次遇到屏幕时,存储的出生月份将从数据库中提取并爆炸。但是八月和九月不会在下拉列表中被选中(但 08 或 09 将根据该位代码回显到屏幕上)。如果用户点击下拉列表(月份),选择 8 月或 9 月并点击 savebtn,8 月或 9 月将不会在下拉列表中正确选择(但 08 或 09 将根据该位代码回显到屏幕上)。哦,当应该选择 8 月或 9 月时,选择了 1 月。非常感谢您提前!

// Basically, if the savebtn was clicked:
$birthmonth = mysqli_real_escape_string($dbc, trim($_POST['birthmonth']));
$birthday = mysqli_real_escape_string($dbc, trim($_POST['birthday']));
$birthyear = mysqli_real_escape_string($dbc, trim($_POST['birthyear']));
$birthdate = $birthyear. '-' . $birthmonth. '-' . $birthday;
echo '<p>The savebtn was clicked. Birthdate: ' . $birthdate . '</p>';
echo '<p>The savebtn was clicked. Birthmonth: ' . $birthmonth . '</p>';

// Basically, if the savebtn was NOT clicked
$birthdate = $row['birthdate'];
if (!empty($birthdate))
list($birthyear, $birthmonth, $birthday) = explode('-', $birthdate);
echo '<p>The savebtn was NOT clicked. DB birthdate: ' . $birthdate . '</p>';
echo '<p>The savebtn was NOT clicked. DB birthmonth: ' . $birthmonth . '</p>';

<form name="usersform" id="usersform" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="post">
<label for="birthmonth" class="label2">Month:</label>
<select name="birthmonth" id="birthmonth">
<?php
if (!empty($birthmonth) && ($birthmonth == 01))
    echo '<option selected="selected" value="01">January</option>';
else
    echo '<option value="01">January</option>';
if (!empty($birthmonth) && ($birthmonth == 02))
    echo '<option selected="selected" value="02">February</option>';
else
    echo '<option value="02">February</option>';
if (!empty($birthmonth) && ($birthmonth == 03))
    echo '<option selected="selected" value="03">March</option>';
else
    echo '<option value="03">March</option>';
if (!empty($birthmonth) && ($birthmonth == 04))
    echo '<option selected="selected" value="04">April</option>';
else
    echo '<option value="04">April</option>';
if (!empty($birthmonth) && ($birthmonth == 05))
    echo '<option selected="selected" value="05">May</option>';
else
    echo '<option value="05">May</option>';
if (!empty($birthmonth) && ($birthmonth == 06))
    echo '<option selected="selected" value="06">June</option>';
else
    echo '<option value="06">June</option>';
if (!empty($birthmonth) && ($birthmonth == 07))
    echo '<option selected="selected" value="07">July</option>';
else
    echo '<option value="07">July</option>';
if (!empty($birthmonth) && ($birthmonth == 08))
    echo '<option selected="selected" value="08">August</option>';
else
    echo '<option value="08">August</option>';
if (!empty($birthmonth) && ($birthmonth == 09))
    echo '<option selected="selected" value="09">September</option>';
else
    echo '<option value="09">September</option>';

if (!empty($birthmonth) && ($birthmonth == 10))
    echo '<option selected="selected" value="10">October</option>';
else
    echo '<option value="10">October</option>';
if (!empty($birthmonth) && ($birthmonth == 11))
    echo '<option selected="selected" value="11">November</option>';
else
    echo '<option value="11">November</option>';
if (!empty($birthmonth) && ($birthmonth == 12))
    echo '<option selected="selected" value="12">December</option>';
else
    echo '<option value="12">December</option>';
?>
</select><br />
<input type="submit" class="submitbtn" name="savebtn" id="savebtn" value="Save" />

4

2 回答 2

2

数字前导 0 使 PHP 将它们解释为八进制。08 和 09 不是有效的八进制数。摆脱前导零。

于 2013-10-10T01:03:59.680 回答
0

为了提供我在评论中建议的三元的例子:

echo '<option value="01"'. !empty($birthmonth) && ($birthmonth == 1) ? ' selected="selected"' : '' .'>January</option>';

您的 IDE 抱怨 PHP 和选项标签可能是另一回事。PHP只是将option标签回显到浏览器,xhtml过渡支持option,所以基本构造应该没有问题。如果在应用这些更新后仍然抱怨,您应该在您的问题中发布您的输出 html 以进行进一步调试。

这个答案只是为了详细说明评论。尽管该示例还应用了 john 提到的更正,但不应将其视为官方答案。

于 2013-10-10T01:47:26.893 回答