Skip to content Skip to sidebar Skip to footer

How To Remember Posted Fields In Forms For Drop Down Items?

I'm creating a simple registration page:

Solution 1:

At the top:

$birthdayMonth = $_POST['birthdayMonth']

In the select:

<select name="birthdayMonth">
    <option value="-1">Month:</option>
    <option value="01"<?php echo $birthdayMonth == '01' ? 'selected="selected"' : ''; ?>>Jan</option>
    <option value="02"<?php echo $birthdayMonth == '02' ? 'selected="selected"' : ''; ?>>Feb</option>
    <option value="03"<?php echo $birthdayMonth == '03' ? 'selected="selected"' : ''; ?>>Mar</option>
    <option value="04"<?php echo $birthdayMonth == '04' ? 'selected="selected"' : ''; ?>>Apr</option>
    <option value="05"<?php echo $birthdayMonth == '05' ? 'selected="selected"' : ''; ?>>May</option>
    <option value="06"<?php echo $birthdayMonth == '06' ? 'selected="selected"' : ''; ?>>Jun</option>
    <option value="07"<?php echo $birthdayMonth == '07' ? 'selected="selected"' : ''; ?>>Jul</option>
    <option value="08"<?php echo $birthdayMonth == '08' ? 'selected="selected"' : ''; ?>>Aug</option>
    <option value="09"<?php echo $birthdayMonth == '09' ? 'selected="selected"' : ''; ?>>Sep</option>
    <option value="10"<?php echo $birthdayMonth == '10' ? 'selected="selected"' : ''; ?>>Oct</option>
    <option value="11"<?php echo $birthdayMonth == '11' ? 'selected="selected"' : ''; ?>>Nov</option>
    <option value="12"<?php echo $birthdayMonth == '12' ? 'selected="selected"' : ''; ?>>Dec</option>
</select>

Solution 2:

This is a really dirty of what doing what you want, but it will work:

http://www.plus2net.com/php_tutorial/pb-drop.php

It would be better to use loop to build the dropdown and then add the selected="selected" value to the correct option.


Solution 3:

<option value="03" <?php echo $_POST['birthdayMonth'] == '03' ?
'selected="selected"' : '' ?>>Mar</option>

Post a Comment for "How To Remember Posted Fields In Forms For Drop Down Items?"