雖然我意識到這不是最有效的代碼,但它不會在八月或九月!我使用PHP 5.4和XHTML 1.0 Transitional。 我已經在持續的IE,Firefox和Google瀏覽器中測試過它。我檢查了發送給瀏覽器的代碼,並且在八月或九月沒有什麼不同。所有其他的月份工作正常。 當用戶第一次遇到屏幕時,存儲的birthmonth將從數據庫中提取並爆炸。但是8月和9月不會在下拉列表中選擇(但是08或09會按照該代碼位置回顯到屏幕上)。 如果用戶點擊下拉列表(月),選擇8月或9月,並點擊savebtn,8月或9月將不會在下拉列表中正確選擇(但08或09將被回顯到屏幕上那一點代碼)。 哦,並且應該選擇8月或9月時,選擇1月。 非常感謝您提前!在數字使用PHP,爲什麼選擇的選項不總是工作?
// 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" />
你真的應該看看三元運營商:http://php.net/manual/en/language.operators.comparison.php –
是的,你是對的。現在問題已解決,我會用三元運算符重新編碼。謝謝。 – user2646006
– user2646006