php - Refilling drop down menu selection after form submission validation -
i'm working on sign up/registration form in php resubmits/retains users input if doesn't validate properly. i've got text box, password input, , radio buttons working these drop down menus have been more trouble. php code used works text boxes not these select/options, there better way this? i've cut out majority of options save space, each goes 0-11 months, 1-31 days, , 1900-2013 years respectively.
<select id="month" name="month" value="<?php if(isset($_post['month'])) echo htmlspecialchars($_post['month'])?>"> <option value="default">month</option> <option value="0">january</option> ... <option value="11">december</option> </select> <select id="formday" name="day" value="<?php if(isset($_post['day'])) echo htmlspecialchars($_post['day'])?>"> <option value="default">day</option> <option value="1">1</option> ... <option value="31">31</option> </select> <select id="formyear" name="year" value="<?php if(isset($_post['year'])) echo htmlspecialchars($_post['year'])?>"> <option value="default">year</option> <option value="2013">2013</option> ... <option value="1900">1900</option> </select>
you may try this, generate values dynamically
day:
echo "<select name='day'>"; for( $i = 1; $i <= 31; $i++ ) { $selectedday = isset($_post['day']) && $_post['day'] == $i ? 'selected="selected"' : ''; echo "<option $selectedday value=$i>$i</option>"; } echo "</select>"; month:
$months = array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'); echo "<select name='month'>"; for( $i = 0; $i <= 11; $i++ ) { $m = $months[$i]; $selectedmonth = isset($_post['month']) && $_post['month'] == $i ? 'selected="selected"' : ''; echo "<option $selectedmonth value=$i>$m</option>"; } echo "</select>"; year:
echo "<select name='year'>"; for( $i = 2013; $i >= 1900; $i-- ) { $selectedyear = isset($_post['year']) && $_post['year'] == $y ? 'selected="selected"' : ''; echo "<option $selectedyear value=$i>$i</option>"; } echo "</select>";
Comments
Post a Comment