Make an option stay selected

Thread Solved

Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Make an option stay selected

 
0
  #1
Feb 22nd, 2008
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.


  1. SQL = "SELECT * FROM tbregion ORDER BY region ASC"
  2.  
  3. Set RS = Server.CreateObject("ADODB.recordset")
  4. RS.Open SQL,Conn
  5.  
  6. Session("region") = Request.Form("regv")
  7. region = Session("region")
  8. %>
  9.  
  10. <form name="region" action="select_menu.asp" method="post">
  11. <select name="regv" onchange="region.submit()">
  12.  
  13. <option value="">Make a selection</option>
  14. <option value="">------------------</option>
  15.  
  16. <%
  17.  
  18. RegID = RS("regionID")
  19.  
  20. x=0
  21. Do While Not RS.EOF
  22.  
  23. If RegID = region Then
  24. For i = 1 To Region
  25. Response.Write("<option value='" & RS("regionID") & "' SELECTED>" & RS("region") & "</option>")
  26. x=x+1
  27. Next
  28.  
  29. Else
  30.  
  31. x=x -1
  32. For i = 1 To RegID
  33. Response.Write("<option value='" & RS("regionID") & "'>" & RS("region") & "</option>")
  34. Next
  35. End If
  36.  
  37.  
  38. RS.MoveNext
  39. Loop
  40. RS.Close
  41. Set RS = Nothing
  42. %>
  43.  
  44. </select>
  45. </form>
  46.  
  47. <%
  48. Response.Write("<a href='category.asp?clevel=1&regv="& region & "'>Link</a>")
  49. %>
Last edited by TobbeK; Feb 22nd, 2008 at 8:00 am.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 514
Reputation: techniner is an unknown quantity at this point 
Solved Threads: 19
techniner techniner is offline Offline
Posting Pro

Re: Make an option stay selected

 
0
  #2
Feb 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Re: Make an option stay selected

 
0
  #3
Feb 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Re: Make an option stay selected

 
0
  #4
Feb 22nd, 2008
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.

  1.  
  2. SQL = "SELECT * FROM tbregion ORDER BY region ASC"
  3.  
  4. Set RS = Server.CreateObject("ADODB.recordset")
  5. RS.Open SQL,Conn
  6.  
  7. Dim region
  8.  
  9.  
  10. %>
  11.  
  12. <form name="region" action="select_menu.asp" method="post">
  13. <select name="regv" onchange="region.submit()">
  14.  
  15. <option value="">Make a selection</option>
  16. <option value="">------------------</option>
  17.  
  18. <%
  19.  
  20. Session("region") = Request.Form("regv")
  21. region = Session("region")
  22. regionID = RS("regionID")
  23.  
  24.  
  25.  
  26. If region <> "" Then
  27. If region <> "0" Then
  28.  
  29.  
  30. Do Until Clng(regionID) = Clng(region)
  31.  
  32. Response.Write("<option value=""" & regionID & """SELECTED>" & RS("region") & "</option>")
  33. regionID = regionID + 1
  34. RS.MoveNext
  35. Loop
  36.  
  37.  
  38. Do Until RS.EOF
  39.  
  40. Response.Write("<option value=""" & regionID & """>" & RS("region") & "</option>")
  41. regionID = regionID + 1
  42. RS.MoveNext
  43. Loop
  44.  
  45. End If
  46. End If
  47.  
  48.  
  49. If region = "" OR region = "0" Then
  50. Do Until RS.EOF
  51. Response.Write("<option value=""" & regionID & """>" & RS("region") & "</option>")
  52. regionID = regionID + 1
  53. RS.MoveNext
  54. Loop
  55.  
  56. End If
  57. %>
  58.  
  59. </select>
  60. </form>
  61.  
  62. <%
  63. Response.Write("<a href='category.asp?clevel=1&regv="& region & "'>Link</a>")
  64. %>
Last edited by TobbeK; Feb 22nd, 2008 at 10:09 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Make an option stay selected

 
0
  #5
Feb 22nd, 2008
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>")
%>
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Re: Make an option stay selected

 
0
  #6
Feb 22nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Make an option stay selected

 
0
  #7
Feb 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Make an option stay selected

 
1
  #8
Feb 22nd, 2008
Also, try adding hidden input fields. It should still send the value through querystring with the GET method.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Re: Make an option stay selected

 
0
  #9
Feb 22nd, 2008
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
Last edited by TobbeK; Feb 22nd, 2008 at 12:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Make an option stay selected

 
0
  #10
Feb 22nd, 2008
Put the form as POST, otherwise your querystring will be overridden. Then for your form action put :
  1. action="<%= Request.ServerVariables("SCRIPT_NAME") & "?" & Request.ServerVariables("QUERY_STRING") %>"
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC