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

Select box default

Status
Not open for further replies.

Phailak

Programmer
Apr 10, 2001
142
CA
Hail,

Here's a quick one, on one of my asp pages, I load 4 select boxes with choices taken from a access database. How do you display one default choice per box without adding (or doubling) that item in the select box.

If you have following items A, B, C, D and E and want D to show when page loads. I put the <OPTION selected>D</OPTION> but obviously this is wrong because it doubles the D selection in the list. Is there a way maybe to specify the item number like in VB with a combobox?

Phailak
 
How does that double the selection??? That's the way to do it, short of writing an inline javascript statement that would set it like you want it --

<script>
document.formName.selectName.value = wantedValue;
</script>

But if you have 5 options, and just put &quot;selected&quot; by one of them, that should not make two entries in the box -- just the one -- and it will be selected....

 
Well, this is what I have, I must of screwed up somewhere...

<%@ Language=VBScript %>
<html>
<%
Dim UID
UID = request.querystring(&quot;Player&quot;)
%>
<form name=frmTT method=post action=SavingChr.asp?Player=<%=UID%>>
<head>
<title>Gladiators</title>
</head>

<body text=&quot;#000000&quot; bgcolor=&quot;#C0C0C0&quot; link=&quot;#0000EE&quot; vlink=&quot;#FFFF99&quot; alink=&quot;#FF0000&quot;>

<center><font size=+4>Creating your Gladiator</font><font size=+4></font>
<p><font size=+2>Now we must decide the strength of your character. Below, you have several choices that will define your gladiator. You must first choose a name, one that will reflect your glory in the Emperor's archives. (Let's not exagerate with the length, I say 25 characters should be enough!).</font>
<p><font size=+2>Then simply choose your desired race, each has their own advantage depending on size (Smaller creatures are faster, bigger ones are stronger while average height such as humans are balanced). Weapon represents your weapon of choice that you were raised to fight with. Style is how you fight and sponsor is whom you seek to represent at the tournaments.</font>
<p><font size=+2>Finally, you may reroll your stats as many times as you wish, just press the reroll button of the following page.</font>
<br><br><br>
<%
sql = &quot;select race from races &quot;
Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
Conn.Open &quot;Glad&quot;
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open sql, Conn
%>
<BR>
<BR>
<font size=+3>Choose your race</font>
<BR>
<select NAME=&quot;cmbRaces&quot;>
<%Do while not rs.eof%>
<OPTION value=&quot;<%= rs(&quot;race&quot;) %>&quot;><%=rs(&quot;race&quot;)%>
<OPTION selected>Choose your race</option>
<%rs.movenext%>
<%loop%>
<%rs.close%>
</select>
<%
sql = &quot;select name, bonus from skills &quot;
rs.Open sql, Conn
%>
<BR>
<BR>
<font size=+3>Choose your inate skill</font>
<BR>
<select NAME=&quot;cmbSkills&quot;>
<%Do while not rs.eof%>
<OPTION value=&quot;<%= rs(&quot;name&quot;) %><%= rs(&quot;bonus&quot;) %>&quot;><%=rs(&quot;name&quot;)%><---><%= rs(&quot;bonus&quot;) %>
<%rs.movenext%>
<%loop%>
<OPTION selected>Choose your inate skill</option>
<%rs.close%>
</select>
<%
sql = &quot;select name, bonus from styles &quot;
rs.Open sql, Conn
%>
<BR>
<BR>
<font size=+3>Choose your style of combat</font>
<BR>
<select NAME=&quot;cmbStyles&quot;>
<%Do while not rs.eof%>
<OPTION value=&quot;<%= rs(&quot;name&quot;) %><%= rs(&quot;bonus&quot;) %>&quot;><%=rs(&quot;name&quot;)%><---><%= rs(&quot;bonus&quot;) %>
<%rs.movenext%>
<%loop%>
<OPTION selected>Choose your style of combat</option>
<%rs.close%>
</select>
<%
sql = &quot;select name, bonus from sponsors &quot;
rs.Open sql, Conn
%>
<BR>
<BR>
<font size=+3>Choose your sponsor</font>
<BR>
<select NAME=&quot;cmbSponsors&quot;>
<%Do while not rs.eof%>
<OPTION value=&quot;<%= rs(&quot;name&quot;) %><%= rs(&quot;bonus&quot;) %>&quot;><%=rs(&quot;name&quot;)%><---><%= rs(&quot;bonus&quot;) %>
<%rs.movenext%>
<%loop%>
<OPTION selected>Choose your sponsor</option>
<%
rs.close
conn.close
set rs = nothing
set conn = nothing
%>
</select>
<br><br><br>
<div align=center><input type=submit value=Accept></div><br><br>
</body>
</form>
</html>

So this shows on the page what I want it to show and appears in the choices so I thought, I'll write these defaults in HTML and put the first one of each list as a default, but when I write it in, it's doubled in the list. This is what I mean:
Let's say &quot;Human&quot; is part of the races list, well in the line
<OPTION selected>Choose your race</option> I replaced &quot;Choose your race&quot; by &quot;Human&quot; and Human doubled up in the list???

While I have your attention, I'm at a point now where the user may go many different ways from a page. Let's say I have 5 different buttons which lead to 5 different pages, what would the buttons looks like, can I do...
First button is &quot;Arena&quot; so
<input type=button value=Arena OnClick=Arena.asp>
Now I know that is not the right syntax at all, but is it something just as simple?

Phailak
 
Without wading through that entire code posting -- here are a few general pointers --

If I have a value in a variable that I want to be selected in the list -- and I know that the value will appear in the recordset at some point, then I'll do this:

<select name=whatever>
<option value=none>SELECT ONE</option>
<%
while not rs.eof
response.write(&quot;<option value=&quot; & rs(&quot;value&quot;))
if rs(&quot;value&quot;) = valueImLookingFor then
response.write(&quot; selected&quot;)
end if
response.write(&quot;>&quot; & rs(&quot;value&quot;) & &quot;</option>&quot;)
rs.movenext
wend
%>
</select>

So that the loop looks and compares the value on each iteration -- and when it finds it, it inserts the 'selected' keyword, and we're good to go --

As far as changing the target -- here's another simplified example that is enough to give you the idea of what is going to have to happen --

<input type=button value=&quot;Page One&quot; onClick=&quot;makeJump('pageOne.asp');&quot;>
<input type=button value=&quot;Page Two&quot; onClick=&quot;makeJump('pageTwo.asp');&quot;>

<script language=javascript>
function makeJump(page){
var url = page + anyOtherQueryStringStuffYouNeed
location = url;
}
</script>

So that you attach your querystring information on to the end of the url manually rather than having it automatically happen with a submit button, and the redirect using the javascript statement, 'location=url;' -- and you've effectively created your own 'get' type form submission --

good luck :)
Paul Prewett
 
Thanx.
For the function, makeJump, do you know what it would look like in vbscript? I guess I could always use javascript in this situation but I'd rather keep to vbscript for now whenever possible.

Phailak
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top