I have an array that holds the values of US States. I have populated a drop down menu using the array (which works fine), and want to make the menu hold the selected value when the page is processed.
If the menu was not populated from an array I would do this...
<select name="state" id="state">
<option value="AL"
<?php if(isset($POST[state']) && $POST[state'] == 'AL'){ echo 'selected';}?> ALABAMA</option>
<option value="AK"
<?php if(isset($POST[state']) && $POST[state'] == 'AK'){ echo 'selected';}?> ARKANSAS</option>
etc...
</select>
So I have tried several variations like this...
<?php
$states = array ('AL'=>'ALABAMA', 'AK'=>'ARKANSAS', etc...);
?>
<select name="state" id="state">
<?php
foreach($states as $key => $value) {
echo "<option value=$key
\"<?php if(isset($POST[state']) && $POST[state'] == '$key'){ echo 'selected';}
?>\"
$value</option>";
}
?></select>
With this I get an "unexpected T_ENCAPSED_AND_WHITESPACE" error. I can see that the code doesn't seem right (trying to echo php code within php code). I have also used a similar script to make a drop down sticky when it is populated from MySQL and everything has worked fine.
Little help?
Thanks