# Select input - how to remember value for subsequent form



## JonathanAnon (Dec 17, 2004)

Hi the problem I have is that I need to have a select form input variable passed to a second form in which it will appear as the option selected on the previous form. 

the only way that I know at the moment is to have a piece of code cycle through all of the options on the select options and try to match the passed variable to each value - then when a match is found use the SELECTED value on the input. 

Is there not an easier way of doing this? It seems like a lot of code for doing something so simple.


----------



## Willy Duitt (Oct 17, 2003)

JonathanAnon said:


> Hi the problem I have is that I need to have a select form input variable passed to a second form in which it will appear as the option selected on the previous form.
> 
> the only way that I know at the moment is to have a piece of code cycle through all of the options on the select options and try to match the passed variable to each value - then when a match is found use the SELECTED value on the input.
> 
> Is there not an easier way of doing this? It seems like a lot of code for doing something so simple.


If the two selects are the same you could just pass the *selectedIndex*...
It would be easier to help you if you posted some code or a link tho...

.....Willy


----------



## Rockn (Jul 29, 2001)

What are you writing this in?


----------



## JonathanAnon (Dec 17, 2004)

The variables are passed through a HTML form and handled by a php script. 
The select box contains all of the counties in Ireland, and one blank option also. 
At the moment the only way that I can get this to work is to use the following code.

When outputting each option using the tag, I have to check if the value of the $county variable (which is passed from the html form) is equal to any of the 32 counties on the list and if it is then it the selected value is added to the option tag. Below is a sample of the code.

echo("");
if(strcmp($county,"")==0) echo(""); 
else echo("");
if(strcmp($county,"Antrim")==0) echo("Antrim"); 
else echo("Antrim");
if(strcmp($county,"Armagh")==0) echo("Armagh"); 
else echo("Armagh");
if(strcmp($county,"Carlow")==0) echo("Carlow"); 
else echo("Carlow");
....................
....................

It is a bit long winded, surely there is a shorter way of doing this ???


----------



## treydx (Jan 4, 2006)

Yeah, you could do this a lot easier! First of all, if you're willing to rely on Javascript to do it for you, then you don't have to echo out all your HTML (which will clean up your code significantly). On the other hand, if a user has Javascript disabled, then you're doing it the only way possible that will work--adding the selected attribute with php. With that being said, you could still create your select list with an array of countries and a loop to print them out. It would shorten up your code some.

Anyway, to do this in javascript, you'll want to add a function to your page:

then on your body tag, add ');">
or whatever the correct syntax for php is.


----------



## jiml8 (Jul 3, 2005)

If you are not allowing multiselect from the select box, then instead of using option buttons on the second form use radio buttons. This will let you set the value for the radio button list with the county name selected, and the proper radio button will then automatically be selected.


----------

