Click to See Complete Forum and Search --> : How do I pass a value from a dropdownlistbox to a new page?
I have defined a dropdownlistbox (let's say I named it "MyDropDownListBox") on one page. Now when I select a value from that listbox, I want to jump to another page, passing the selected value (an ID) along in the querystring.
How do I make it do that?
Mark Sreeves
Oct 30th, 2000, 08:05 AM
try this:
<html>
<head></head>
<body>
<form name=frm3 action=some.asp>
<select name=select1 onClick="document.frm3.submit()">
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
</select>
</form>
</body>
</html>
some.asp
<%@ Language=VBScript %>
<HTML>
<HEAD>
</HEAD>
<BODY>
<h1>some asp</h1>
<%
response.write("select 1: " & request("select1") & "<BR>")
%>
</BODY>
</HTML>
Okay, this works... almost.
Now when I release my mousebutton (after clicking it) on the arrow of the dropdownlistbox, it refreshes the page immediately (it redirects to itself). If I want to select a user, I have to hold the button down. This is not how a dropdownlistbox usually works, is it?
Now I don't want the page to refresh untill I select a user from the dropdownlist, not as soon as I have clicked the dropdownlistbox-arrow.
And I would also like the selected user to still be selected after refreshing the page. How do I do that?
Thanx a lot. :)
[Edited by MarcelB on 10-30-2000 at 10:02 AM]
Mark Sreeves
Oct 30th, 2000, 10:17 AM
try this:
the doit() function makes sure the page is not submitted when the arrow is first clicked.
keeping the option selected:
there's lots of different ways to do this
if the value of the options are numeric it's a bit easier see select2
<%@ Language=VBScript %>
<HTML>
<HEAD>
<script language=javascript>
var clickCount = 0;
function doit()
{
if(clickCount==0)
{
clickCount++;
}else{
document.frm3.submit()
}
}
</script>
</HEAD>
<%
dim opt
dim opt2
opt = request("select1")
opt2 = cint(request("select2"))
%>
<BODY>
<form name=frm3 action=some.asp>
<select name=select1 onclick='doit()'>
<%
select case opt
case "Option 2"
response.write "<option value='Option 1'>Option 1</option>" & vbcrlf
response.write "<option value='Option 2' selected>Option 2</option>" & vbcrlf
response.write "<option value='Option 3'>Option 3</option>" & vbcrlf
case "Option 3"
response.write "<option value='Option 1'>Option 1</option>" & vbcrlf
response.write "<option value='Option 2'>Option 2</option>" & vbcrlf
response.write "<option value='Option 3' selected>Option 3</option>" & vbcrlf
case else
response.write "<option value='Option 1' selected>Option 1</option>" & vbcrlf
response.write "<option value='Option 2'>Option 2</option>" & vbcrlf
response.write "<option value='Option 3'>Option 3</option>" & vbcrlf
end select
%>
</select>
<select name=select2 onclick='doit()'>
<option value=0>Option 1</option>
<option value=1>Option 2</option>
<option value=2>Option 3</option>
</select>
</form>
<%
response.write "<script language=javascript>" & vbcrlf
response.write "document.frm3.select2.selectedIndex=" & opt2 & ";" & vbcrlf
response.write "</script>" & vbcrlf
%>
</body>
</html>
Okay, getting closer.. but not yet exactly what I want. (sorry)
Now when a user clicks the arrow, then for some reason clicks it again without having selected an option, it still refreshes the page, which is not what I want. I know this was somewhat of a dirty work-around way (no offense) to solve the problem, but surely there must be a "clean" way too, right? Anyone?
Furthermore in the select box, I have a UserID and a name, where the index in the combobox does not necessarily match the ID of the user. Now when the admin selects the user with UserID = 3, it selects the third user in the dropdownlistbox, instead of the user with UserID = 3.
I also tried the other method (using a variant of the case statement) but that didn't seem to work either. Any other ideas?
Thanx a lot for thinking along, by the way. I really appreciate it. :)
monte96
Oct 30th, 2000, 03:02 PM
Try using the onchange event instead. Have the first option in your select tag be a dummy option that just says "Please select..." or something like that. In your onchange event, do your refresh only if that is not the selected index.
Alrighty! Thanx again, Monty.. once again, you save the day! :)
BUT...... that still leaves me with the problem of having the right option selected after refreshing the page.
Is there some way I could read the index of the selected option instead of the value? Then if I pass that index along, and after the refresh I build the dropdownlistbox the same way I built it before the refresh, the index should point to the selected user, right?
Alrighty!!!! :)
I got that last thing to work aswell, thanx to Mark. I had converted the UserID that I got from the database to an integer, but I hadn't converted the value that was passed along to an integer, so when I compared the two, they weren't the same.
Thanx guys, you're the best! :)
Mark Sreeves
Oct 31st, 2000, 02:49 AM
It's a tad tricky trying to visualise what you want to do!
what you could do is have the data (could do with an example really) in a javascript array and then have numeric option values and then pick out the required element from the array onChange and post it
<%@ Language=VBScript %>
<html>
<head>
<%
dim opt
dim choice
opt = cint(request("select1"))
choice= request("SelectedOption")
%>
<script language=javascript>
var anArray = new Array();
anArray[0] = "Please select";
anArray[1] = "Option 1";
anArray[2] = "Option 2";
anArray[3] = "Option 3";
function doit(v)
{
document.frm3.SelectedOption.value = anArray[v];
document.frm3.submit();
}
</script>
</head>
<body onLoad="document.frm3.select1.selectedIndex=<%=opt%>;">
<form name=frm3 action=some.asp>
<select name=select1 onChange="doit(this.selectedIndex);">
<option value="0">Please select</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<input type=hidden name=SelectedOption>
</form>
<%
Response.Write "you chose " & choice
%>
</body>
</html>
Actually I "already" have it working, Mark, thanx to you and Monty's combined effort.
But thanx for replying anyway. :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.