I thought this was a simple task to do, but not.

I just want the option stay selected when a value from the session variable exists, AND it does after the form has been submitted. So why can't the option value stay selected as long as session is populated.

If I put a static value in the loop like this:
If RegID = 5 Then ...

Then the option stays selected at this post.

SQL = "SELECT * FROM tbregion ORDER BY region ASC"

Set RS = Server.CreateObject("ADODB.recordset")
RS.Open SQL,Conn

Session("region") = Request.Form("regv")
region = Session("region")
%>

<form name="region" action="select_menu.asp" method="post">
<select name="regv" onchange="region.submit()">

<option value="">Make a selection</option>
<option value="">------------------</option>

<%

RegID = RS("regionID")

x=0
Do While Not RS.EOF

If RegID = region Then
For i = 1 To Region
Response.Write("<option value='" & RS("regionID") & "' SELECTED>" & RS("region") & "</option>")
x=x+1
Next

Else

x=x -1
For i = 1 To RegID
Response.Write("<option value='" & RS("regionID") & "'>" & RS("region") & "</option>")
Next
End If


RS.MoveNext
Loop
RS.Close
Set RS = Nothing
%>

</select>
</form>

<%
Response.Write("<a href='category.asp?clevel=1&regv="& region & "'>Link</a>")
%>

Have you tried declaring these?

I have ran into some odd ball issues when comparing text field to integer.

Try setting both as integer then do your compare.

Been there, done that :-)
Does not help

What I tried to in my code is to loop from BOF until the values match and make that post SELECTED, and then keep on looping from that value to EOF.

Now it works, with a small little bug.

If I have selected an option with (let's say value=20) then responded value is 19. So there is a miscalculation that takes the actual value and set it to -1.
I don't know where the bug is.

It is possible to set the session variable to +1 but I really like know where the error is.

SQL = "SELECT * FROM tbregion ORDER BY region ASC"

Set RS = Server.CreateObject("ADODB.recordset")
RS.Open SQL,Conn

Dim region


%>

<form name="region" action="select_menu.asp" method="post">
<select name="regv" onchange="region.submit()">

<option value="">Make a selection</option>
<option value="">------------------</option>

<%

Session("region") = Request.Form("regv")
region = Session("region")
regionID = RS("regionID")



If region <> "" Then
	If region <> "0" Then


Do Until Clng(regionID) = Clng(region)

Response.Write("<option value=""" & regionID & """SELECTED>" & RS("region") & "</option>")
regionID = regionID + 1
RS.MoveNext
Loop


Do Until RS.EOF 

Response.Write("<option value=""" & regionID & """>" & RS("region") & "</option>")
regionID = regionID + 1
RS.MoveNext
Loop

End If
End If


If region = "" OR region = "0" Then
Do Until RS.EOF
Response.Write("<option value=""" & regionID & """>" & RS("region") & "</option>")
regionID = regionID + 1
RS.MoveNext
Loop

End If
%>

</select>
</form>

<%
Response.Write("<a href='category.asp?clevel=1&regv="& region & "'>Link</a>")
%>

You're setting RegID out of the Do While Not RS.EOF, so therefore it is never being set, or it is being set to the first record only.

Move it down to within the do while not rs.eof.

SQL = "SELECT * FROM tbregion ORDER BY region ASC"

Set RS = Server.CreateObject("ADODB.recordset")
RS.Open SQL,Conn

Session("region") = Request.Form("regv")
region = Session("region")
%>

<form name="region" action="select_menu.asp" method="post">
<select name="regv" onchange="region.submit()">

<option value="">Make a selection</option>
<option value="">------------------</option>

<%

RegID = RS("regionID")

x=0
Do While Not RS.EOF

If RegID = region Then
For i = 1 To Region
Response.Write("<option value='" & RS("regionID") & "' SELECTED>" & RS("region") & "</option>")
x=x+1
Next

Else

x=x -1
For i = 1 To RegID
Response.Write("<option value='" & RS("regionID") & "'>" & RS("region") & "</option>")
Next
End If


RS.MoveNext
Loop
RS.Close
Set RS = Nothing
%>

</select>
</form>

<%
Response.Write("<a href='category.asp?clevel=1&regv="& region & "'>Link</a>")
%>

Great as usual, THANKS!

Here's a new question within (ok maybe a little outside the scope of the thread).

The sending form is suppuse to send a value back to itself, that means same page. Now, I need to use the exact URL of the page so that value landing at the same spot again. I have tried to put variables in the form action but doesn't help much.

Example:

http://www.webpage.com?category.asp?id=1&anotherid=2

When sending the form value, the only thing happens is that the form value appends to the URL like this:

http://www.webpage.com?category.asp?regv=5

So I'm not at the same place as before.

Any ideas are extremely welcome

I don't understand, please explain a little more.

By changing the mode to POST for the form, nothing will be put in the querystring. You can then check for the form to see if it was submitted by using the request.form method.

Also, try adding hidden input fields. It should still send the value through querystring with the GET method.

commented: This is the guy +1

OK, It is my swenglish

Let's say i have clicked on a link and landed at this page with this URL and values
Example: category.asp?id=2&otherid=3
Now, this is a page with specific content set by the appended values:

I wan't to send a value from the dropdown form at that page, and land again at exact page content.

What's happends now is that I land at category.asp?regv=5 (the regv=5 is the form value) and the other appended values is lost

This is the form settings:
<form name="region" action="category.asp" method="get">
<select name="regv" onchange="region.submit()">

I have tried use post also:
<form name="region" action="category.asp" method="post">
<select name="regv" onchange="region.submit()">

After sending the form value I like land at the exact URL again
For example: category.asp?id=2&otherid=3

I have tried to change the form action to append variables but no luck. It only sends the form value

Put the form as POST, otherwise your querystring will be overridden. Then for your form action put :

action="<%= Request.ServerVariables("SCRIPT_NAME") & "?" & Request.ServerVariables("QUERY_STRING") %>"

You are the Man :-)

Extraordinary my dear Watson

Many, many many thanks :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.