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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Box doubles

Status
Not open for further replies.

Phailak

Programmer
Apr 10, 2001
142
CA
Hail,

I have a select box on my page that fetches its contents in a database. That part works fine. Actually, I'm sure this is rather a simple problem and I just can't see it. When I want to put one of those as default, it appears double in the list

<font size=+3>Choose your race</font>
<select NAME=&quot;cmbRaces&quot;>
<%Do while not rs.eof%>
<OPTION value=&quot;<%= rs(&quot;race&quot;) %>&quot;><%=rs(&quot;race&quot;)%>
<%rs.movenext%>
<%loop%>
<OPTION SELECTED VALUE = &quot;Human&quot;>Human</OPTION>
</select>
<%rs.close%>

Amongst the races, I want Human to appear in the box by default. It does this way but appears twice in the list. How would I correct this

Phailak
 
howdy,

here is what i would do:

<select NAME=&quot;cmbRaces&quot;>
<%

dim str

str=&quot;&quot;

Do while not rs.eof

str=str & &quot;<option value='&quot; & rs(&quot;race&quot;) & &quot;'&quot;
if lcase(rs(&quot;race&quot;))= &quot;human&quot; then str=str & &quot; selected&quot;
str=str & &quot;>&quot; & rs(&quot;race&quot;) & &quot;</option>&quot;

rs.movenext

loop

response.write str
%>
</select>

i took a com class a few months ago and the instructor said you would get better performance with one response.write rather than several, which makes sense. the <%= is equivalent to a response.write.

hope this helps.
 
Hail,

Looks good, but I get an empty box, no values at all.
I get the idea though, I'll try playing around with it

Phailak
 
you could run into a problem if your 'races' have single or double quotes in them. if that is the case, replace the line:

str=str & &quot;<option value='&quot; & rs(&quot;race&quot;) & &quot;'&quot;

with:

str=str & &quot;<option value=&quot; & chr(34) & rs(&quot;race&quot;) & chr(34)
 
Hail,

No, there aren't any quotes. It's weird, I'm sure I'm missing something simple.

Phailak
 
since your Value is the same as your Option, you could do it this way:

Do while not rs.eof

str=str & &quot;<option&quot;
if lcase(rs(&quot;race&quot;))= &quot;human&quot; then str=str & &quot; selected&quot;
str=str & &quot;>&quot; & rs(&quot;race&quot;) & &quot;</option>&quot;

rs.movenext

loop

if race is a memo field, i have noticed in the past that the value of a memo field can only be accessed once. if that is the case, try this way:

dim str, strRace

Do while not rs.eof

strRace=&quot;&quot; & rs(&quot;race&quot;)

str=str & &quot;<option&quot;
if lcase(strRace)= &quot;human&quot; then str=str & &quot; selected&quot;
str=str & &quot;>&quot; & strRace & &quot;</option>&quot;

rs.movenext

loop
 
Hail,

Ok I got it, wonder if you can help me on the following aspect.

I made a reroll button, which simply redirects the page to itself, changing the stats, but I don't want to change the select boxes. I put this function in, but it only works if the user changed the box, else it returns an empty string. Basically, I want to post the text displayed in the box

<div align=center><input type=button value=&quot;ReRoll&quot; ONCLICK = &quot;ReRoll()&quot;></div><br><br>

<SCRIPT LANGUAGE=&quot;VBScript&quot;>
Function ReRoll()
document.frmTT.action = &quot;CreateChr.asp?Player=<%=UID%>&cRace=<%=request.form(&quot;cmbRaces&quot;)%>&cSkill=<%=request.form(&quot;cmbSkills&quot;)%>&cStyle= <%=request.form(&quot;cmbStyles&quot;)%>&cSponsor=<%=request.form(&quot;cmbSponsors&quot;)%>&cReRoll=YES&quot;
document.frmTT.submit
end function
</SCRIPT>

Any ideas?

Phailak
 
ok, here ya go, but there are some conditions.

you must have a form tag. i assume you do, but you didn't show it in your code.

you must use the value parameter on your option tags.

if you change the name of the form, make sure you change the doucment.frmtt referenece in the ReRoll subroutine.

this should work:

<SCRIPT LANGUAGE=&quot;VBScript&quot;>
sub ReRoll()

with document.frmtt

.action=&quot;CreateChr.asp?Player=<%=UID%>&cRace=&quot; + .cmbRaces.value + &quot;&cSkill=&quot; + .cmbSkills.value + &quot;&cStyle=&quot; + .cmbStyles.value + &quot;&cSponsor=&quot; + .cmbSponsors.value + &quot;&cReRoll=YES&quot;
.submit

end with

end sub
</SCRIPT>

there is an alternative method, which might be easier. when you submit this page, the values are going to be stored in request.form(&quot;cmdRaces&quot;) and so on anyway. since you are going to be checking for request.querystring(&quot;cRace&quot;), it's just as easy to check for request.form(&quot;cmdRaces&quot;). to do this, you would change your <input type=button> to <input type=submit> and you wouldn't need the vbscript routine at all.

also, to insure that the same page is called, you could replace CreateChr.asp with <%=request.servervariables(&quot;script_name&quot;)%>. that way you are guaranteed that the same page is going to be called and makes your code more portable.

hope this helps rather than confuse.
 
Hail,

Actually this is good stuff, it clears a lot, I'll try it let you know, thanx a lot

Phailak
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top