Script to pre-select state drop down box from banner/adserver geotargeting variable

Sonny Forelli

Well-known member
May 8, 2008
2,330
89
48
Liberty City
Anyone have a php snippet they could share?

Issue I'm having is the if/else regarding <if state variable present> <THAT STATE selected="selected">STATE NAME</> and else <"">Choose a state...</>

Can anyone help me out w/ an easy way to do this since I'm coding challenged?

Merry Kwanza
 


Is this the right sort of thing?
PHP:
<?
if($state != '')
{
?>
<option value="<?=$state?>" selected="selected"><?=$state?></option>
<?	
}
else
{
?>
<option value="state">STATE NAME</option>
<option value="state1">STATE NAME 1</option>
<option value="state2">STATE NAME 2</option>
<option value="state3">STATE NAME 3</option>
<option value="state4">STATE NAME 4</option>
<?
}
?>

ps. my php coding is very relaxed
 
that shouldn't work correctly.

make an array of all states, then a foreach loop that goes through the array of states, checking each one to see if it's the one that should be "selected"..if it is, add the selected tag, otherwise, just echo the standard option tag