Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

drop down box persistance in a form

Status
Not open for further replies.

price79

IS-IT--Management
Sep 24, 2004
9
US
I am trying to make a selection box keep the selected value after the page is reloaded.

This is a form for people to select which year of car they want to search on. There is a keyword and then an onsubmit function which attaches a 2 letter year to the keyword and submits it to the search page. The user has to select the keyword from a dropdown selection box.

After the search is performed, the person is taken to the results, but the selection box resets. When people search again and again, they have to keep opening the dropdown and selecting their year. any ideas how to keep the selection after the search is done?

Here is the FORM Code

-----------------------------------------------------------

<form id="find_spec" name="find_spec" method="post" action="<*SearchResultspageURL*>" onsubmit="appendgeneration();">

<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input type="hidden" id="searchtype" name="searchtype" value="AND"><input name="find_spec" type="text" class="searchbox1" id="keyword" onFocus="this.value=''" value=" Part# or Keyword(s)" maxlength="50">
</td>
<td nowrap>
<img src="<*ImageURL*>filltop.gif" width="2" height="1">
<select class="searchbox" id="generation">
<option value="Corvette" selected>Year</option>
<option value="Corvette">ALL</option>
<option value="07">2007</option>
<option value="06">2006</option>
<option value="05">2005</option>
<option value="04">2004</option>
<option value="03">2003</option>
<option value="02">2002</option>
<option value="01">2001</option>
<option value="00">2000</option>
<option value="99">1999</option>
<option value="98">1998</option>
<option value="97">1997</option> </select></td>
<td align="center" valign="middle" nowrap><input name="btnSearch" type="image" id="btnSearch2" value="Search" src="<*ImageURL*>go3.gif" width="40" height="20"></td>
</tr>
</table>
</form></div>
----------------------------------------------------------

I've been told to do it by simply outputting selected="selected" at the option that was chosen.

I need more information, what is the javascript function code? I have no knowledge of writing javascript.
 
do you dynamically generate this code and if so with what language.

to loop a select element and set it you can use.

Code:
var obj = document.getElementById('myid');
for(x=0; x<= obj.length; x++){

  if(obj[x].value == "myval"){
    obj[x].selected = true;
  }

}

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
No, the code is not dynamic, it is static in the header of every page on my site.
 
so how do you display search results if the page isn't built dynamic dependent on search criteria?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
The results page does display the results of the search based on the criteria selected, but nothing in the form code or header of the page is changed, only the body of the page changes.
 
ok in the search area of the page put
Code:
<script type="text/javascript">
var myselected = [b]"THIS NEEDS TO BE THE VALUE PARSED BY THE SERVER OF THE SELECTED SELECT LIST ITEM"[/b];

function setselected(){

var obj = document.getElementById('generation');
for(x=0; x<= obj.length; x++){
  if(obj[x].value == myselected){
    obj[x].selected = true;
  }

}

}
</script>

and in the body tag
Code:
<body onload="setselected();">

I still don't know what serverside language you are using so cannot help with how you get the value of the selected item when the form is submitted, but you must know what it is to evaluate it then display the search results.


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I can still not get this to work, other projects were more important, but now I need to see if someone can help me get this done.

Here is the URL of the website that I am currently using the search on: Maybe you can have a look there and let me know if there is an easy way to do this. I only have access to the page code, nothing on the server side.

Thanks!
 
price79, you are using ASP, which is a serverside language. That's what 1DMF was referring to above.

1DMF said:
I still don't know what serverside language you are using so cannot help with how you get the value of the selected item when the form is submitted, but you must know what it is to evaluate it then display the search results.

[monkey][snake] <.
 
Thanks,
I'm not savvy with code/server stuff.
 
Well in the pulldown 'shop by brand' I select 'Wilwood' , I click go, the server reads what selection is made via the pull down and queries the database, the results are then displayed on the page.

When outputting the page, the code i supplied that goes in the head needs to be written
Code:
<script type="text/javascript">
var myselected = "wilwood";

function setselected(){

var obj = document.getElementById('keyword');
for(x=0; x<= obj.length; x++){
  if(obj[x].value == myselected){
    obj[x].selected = true;
  }

}

}
</script>

you need to dynamically create the 'wilwood' or what ever the selection was made and put the text in.

That should do the job.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I understand how to get this to work, but I have no way to dynamically create the "generation" field. I do not have any access to the server.

Is there an easy way to save this dropdown as a cookie?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top