Skip to content
Advertisement

With PHP, why does option selected not always work?

While I realize that this isn’t the most efficient bit of code, it will not work for August or September! I’m using PHP 5.4 and XHTML 1.0 Transitional. I’ve tested it in the lasted IE, Firefox and Google browsers. I’ve checked the code that is sent to the browsers and there isn’t anything different about August or September. All the other months work fine. When the user first encounters the screen, the stored birthmonth will be extracted from the database and exploded. But August and September will not be selected on the drop down list (but 08 or 09 will be echo-ed to the screen per that bit of code). If the user clicks the drop down list (for month), chooses August or September and clicks the savebtn, August or September will not be selected on the drop down list properly (but 08 or 09 will be echo-ed to the screen per that bit of code). Oh, and when August or September are supposed to be selected, January is selected. Thank you very much ahead of time!

// 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" />

Advertisement

Answer

Leading 0s on numbers make PHP interpret them as octal. 08 and 09 are not valid octal numbers. Get rid of the leading zeros.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement