> 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.???

A trick I picked up from someone on this list:

<?php
$$state = ' selected="true"';
print <<<END
 <select name="state">
             <option value="AL"$AL>Alabama</option>
             <option value="AK"$AK>Alaska</option>
             <option value="AZ"$AZ>Arizona</option>
             <!-- . . . -->
 </select>
END;
?>

That's it!  If $state is set to 'AZ', then the $$state line creates a
variable called $AZ, and sets its value to ' selected="true"'.  Running
the code produces this HTML:

 <select name="state">
             <option value="AL">Alabama</option>
             <option value="AK">Alaska</option>
             <option value="AZ" selected="true">Arizona</option>
             <!-- . . . -->
 </select>

This solution might be considered ugly for several reasons:

  * all sorts of warnings are thrown, one for each unset state variable
  * it's entirely un-obvious to the casual code-reviewer what's going
    on.  So it requires more documentation.
  * you run the risk of stepping on pre-existing variables.

However, the upside, namely, eliminating a *ton* of code, is attractive
enough to me that I use this for most select boxes I do.

Joel

-- 
[ joel boonstra | [EMAIL PROTECTED] ]


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

Reply via email to