Remember Selected Option
I'm making a form in PHP that remembers what the user entered/selected.
For the state drop down, I want the script to remember what they entered in. The problem is, the way I did it seems really inefficient. Take a look:
<?php
function selected($state) {
if ($_SESSION[bill_state] == $state) {
echo \"selected=\\"selected\\"\";
}
}
?>
And the form
<select name="bill_state">
<optgroup label="States">
<option value='AK' <?php selected(AK) ?>>Alaska</option>
<option value='AL' <?php selected(AL) ?>>Alabama</option>
<option value='AZ' <?php selected(AZ) ?>>Arizona</option>
...
So for every state, the script runs through one if statement! That's 50 ifs for one simple thing. Is there a better way?
Greg K posted this at 19:44 — 27th March 2007.
He has: 2,145 posts
Joined: Nov 2003
As states are something that can be used again and again, so I have an included file for this:
<?php
$arStateData = array();
$arStateData['AL'] = \"Alabama\";
$arStateData['AK'] = \"Alaska\";
$arStateData['AZ'] = \"Arizona\";
$arStateData['AR'] = \"Arkansas\";
$arStateData['CA'] = \"California\";
$arStateData['CO'] = \"Colorado\";
$arStateData['CT'] = \"Connecticut\";
$arStateData['DC'] = \"District of Columbia\";
$arStateData['FL'] = \"Florida\";
$arStateData['GA'] = \"Georgia\";
$arStateData['HI'] = \"Hawaii\";
$arStateData['ID'] = \"Idaho\";
$arStateData['IL'] = \"Illinois\";
$arStateData['IN'] = \"Indiana\";
$arStateData['IA'] = \"Iowa\";
$arStateData['KS'] = \"Kansas\";
$arStateData['KY'] = \"Kentucky\";
$arStateData['LA'] = \"Louisiana\";
$arStateData['ME'] = \"Maine\";
$arStateData['MD'] = \"Maryland\";
$arStateData['MA'] = \"Massachusetts\";
$arStateData['MI'] = \"Michigan\";
$arStateData['MN'] = \"Minnesota\";
$arStateData['MS'] = \"Mississippi\";
$arStateData['MO'] = \"Missouri\";
$arStateData['MT'] = \"Montana\";
$arStateData['NE'] = \"Nebraska\";
$arStateData['NV'] = \"Nevada\";
$arStateData['NH'] = \"New Hampshire\";
$arStateData['NJ'] = \"New Jersey\";
$arStateData['NM'] = \"New Mexico\";
$arStateData['NY'] = \"New York\";
$arStateData['NC'] = \"North Carolina\";
$arStateData['ND'] = \"North Dakota\";
$arStateData['OH'] = \"Ohio\";
$arStateData['OK'] = \"Oklahoma\";
$arStateData['OR'] = \"Oregon\";
$arStateData['PA'] = \"Pennsylvania\";
$arStateData['RI'] = \"Rhode Island\";
$arStateData['SC'] = \"South Carolina\";
$arStateData['SD'] = \"South Dakota\";
$arStateData['TN'] = \"Tennessee\";
$arStateData['TX'] = \"Texas\";
$arStateData['UT'] = \"Utah\";
$arStateData['VT'] = \"Vermont\";
$arStateData['VA'] = \"Virginia\";
$arStateData['WA'] = \"Washington\";
$arStateData['WV'] = \"West Virginia\";
$arStateData['WI'] = \"Wisconsin\";
$arStateData['WY'] = \"Wyoming\";
/**
* Form Select of States
*
* @param string $preSel The state that should be selected already
* @param string $selName The name to give the SELECT
* @param string $selAtt Any optional attributes for the SELECT tag (eg. class='whatever' )
* @param string $optAtt Any optional attributes for all OPTION tage
* @param string $curOptAtt Any optional attributes for the already selected OPTION
*/
function selState($preSel = '', $selName = 'state', $selAtt = '', $optAtt = '', $curOptAtt = '')
{
if ($curOptAtt == '') $curOptAtt = $optAtt;
echo \"<select name=\\"\" . $selName . \"\\" \" . $selAtt . \">\n\";
foreach ($GLOBALS['arStateData'] as $stAbbr => $stName)
{
echo '<option value=\"' . $stAbbr . '\" ';
if ($preSel == $stAbbr)
echo 'selected=\"selected\" ' . $curOptAtt;
else
echo $optAtt;
echo '>' . $stName . \"</option>\n\";
}
echo \"</select>\n\";
}
?>
<?php
include_once('kgSelStates.inc');
selState('OH','mySate', 'class=\"states\"', 'class=\"items\"', 'class=\"theItem\"');
?>
See the comments before the function and the function defining line to see what the parameters are and their defaults, all are optional. Note that if you do not use the $curOptAtt parameter, it will default back to the value of $optAtt. (done in first line of function)
NOTE you will need to modify the function a bit to include the tag. Excluding the need for that, here is how you could use it based on your example:
<?php
selState($_SESSION['bill_state'],'bill_state')
?>
Also, note, that in the example above bill_state is enclosed in quotes as it should be, you left them out in your example. (see http://www.php.net/manual/en/language.types.array.php#language.types.array.donts )
-Greg
teammatt3 posted this at 20:43 — 27th March 2007.
He has: 2,102 posts
Joined: Sep 2003
Sweet deal Greg thanks a lot.
Crap, no, the below isn't right, give me a minute to figure it out again
EDIT Again forgot to change a var name. Now it should work
I modified it to add in the Canadian Provinces. Here's how, and I have no idea if it's the best way to do it, but it works for me.
Make a new array
<?php
$arProvinceData = array();
$arProvinceData['AB'] = \"Alberta\";
$arProvinceData['BC'] = \"British Columbia\";
$arProvinceData['MB'] = \"Manitoba\";
$arProvinceData['NB'] = \"New Brunswick\";
$arProvinceData['NL'] = \"Newfoundland and Labrador\";
$arProvinceData['NT'] = \"Northwest Territories\";
$arProvinceData['NU'] = \"Nunavut\";
$arProvinceData['ON'] = \"Ontario\";
$arProvinceData['PE'] = \"Prince Edward Island\";
$arProvinceData['QC'] = \"Québec\";
$arProvinceData['SK'] = \"Saskatchewan\";
$arProvinceData['YT'] = \"Yukon\";
?>
Change Greg's function a little by adding a few echos and another foreach loop. I want the optgroups
<?php
function selState($preSel = '', $selName = 'state', $selAtt = '', $optAtt = '', $curOptAtt = '')
{
if ($curOptAtt == '')
{
$curOptAtt = $optAtt;
}
echo \"<select name=\\"\" . $selName . \"\\" \" . $selAtt . \">\n\";
echo \"<optgroup label=\\"States\\">\";
foreach ($GLOBALS['arStateData'] as $stAbbr => $stName)
{
echo '<option value=\"' . $stAbbr . '\" ';
if ($preSel == $stAbbr)
{
echo 'selected=\"selected\" ' . $curOptAtt;
}
else
{
echo $optAtt;
}
echo '>' . $stName . \"</option>\n\";
}
echo \"</optgroup>\";
echo \"<optgroup label=\\"Provinces\\">\";
foreach ($GLOBALS['arProvinceData'] as $prAbbr => $prName)
{
echo '<option value=\"' . $prAbbr . '\" ';
if ($preSel == $prAbbr)
{
echo 'selected=\"selected\" ' . $curOptAtt;
}
else
{
echo $optAtt;
}
echo '>' . $prName . \"</option>\n\";
}
echo \"</select>\n\";
}
?>
Thanks again Greg
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.