What I would do, and this is one of those "write it once, use it a 1000
times" things, is store all your states in an array.

<?
$states = array(
            'AL' => 'Alabama',
            'AK' => 'Alaska',
            'AZ' => 'Arizona'
            );
?>

Then, you can loop through that array to build your form element:

<?
echo '<select name="state">';
foreach($states as $code => $fullName)
    {
    echo "<option value='{$code}'>{$fullName}</option>";
    }
echo '</select>';
?>

MUCH less code eh?

Then, to have the pre-selected element selected again, I'll assume you have
a value like $state or $selectedState, with a value of 'AZ' for example.

<?
$selectedState = 'AZ'; //usually grabbed from a database I assume?

echo '<select name="state">';
foreach($states as $code => $fullName)
    {
    if($stateSelected == $code) { $sel = ' SELECTED'; } else { $sel = ''; }
    echo "<option value='{$code}'{$sel}>{$fullName}</option>";
    }
echo '</select>';
?>

The result of which would be something like:

<select name="state">
<option value='AL'>Alabama</option>
<option value='AK'>Alaska</option>
<option value='AZ' SELECTED>Arizona</option>
</select>


Season to taste :)

Justin French




on 07/01/03 4:54 AM, Steven Kallstrom ([EMAIL PROTECTED]) wrote:

> I have a drop down list with all fifty states. very common.  I conjured
> up a way to store the value when you return to edit the form, but there
> most be an easier way either in html, or in php.  Here is what I
> currently have.
> 
> <select name="state">
> <option value="AL"{$stateselected['AL']}>Alabama</option>
> <option value="AK"{$stateselected['AK']}>Alaska</option>
> <option value="AZ"{$stateselected['AZ']}>Arizona</option>
> .
> </select>
> 
> $stateselected['$state'] is an array that stores the state that was
> selected on the prior form.  is there an easier way, to have a default
> state picked out of this drop down list.???
> 
> Thanks,
> 
> Steven Kallstrom
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to