Connecting Tech Pros Worldwide Forums | Help | Site Map

How do you show Dropdown menu selected value in edit user page

Member
 
Join Date: Oct 2008
Location: Cleveland Ohio
Posts: 67
#1: Nov 4 '08
Hello,

I have an edit user page that allows the user to view their user information and make changes if possible. I have a simple html login page that directs to an asp page called edituser.asp when they login. Here is the edituser.asp code I have

Expand|Select|Wrap|Line Numbers
  1. <%@ Language=VBScript %>
  2. <% Option Explicit %>
  3. <!--#include virtual="/adovbs.inc"-->
  4. <html>
  5. <body>
  6. <%
  7. Dim oConn, oRs
  8. Dim connectstr, sDSNDir, tablename
  9. Dim db_name, db_username, db_userpassword
  10. Dim dsn_name
  11.  
  12. dsn_name = "register.dsn"
  13. tablename = "tblRegister"
  14. db_username = "****"
  15. db_userpassword = "****"
  16.  
  17. sDSNDir = Server.MapPath("/_dsn")
  18. connectstr = "filedsn=" & sDSNDir & "/" & dsn_name
  19.  
  20. Set oConn = Server.CreateObject("ADODB.Connection")
  21. oConn.Open connectstr
  22.  
  23. Dim objRS, bolFound, strEmail
  24. strEmail = Request.Form("email")
  25.  
  26. If strEmail = "" Then
  27. oConn.Close
  28. Set objConn = Nothing
  29. Response.Write "<a href='login.html'>"
  30. Response.Write "You must enter a email address"
  31. Response.Write "</a>"
  32. Response.End
  33. End If
  34.  
  35. Set objRS = Server.CreateObject("ADODB.Recordset")
  36. objRS.Open "tblRegister", oConn, , , adCmdTable
  37. bolFound = False
  38.  
  39. Do While Not (objRS.EOF OR bolFound)
  40. If (StrComp(objRS("Email"), strEmail, vbTextCompare) = 0) Then
  41. BolFound = True
  42. Else
  43. objRS.MoveNext
  44. End If
  45. Loop
  46.  
  47. If Not bolFound Then
  48. objRS.Close
  49. Set objRS = Nothing
  50. oConn.Close
  51. Set oConn = Nothing
  52. Response.Write "<a href='login.html'>"
  53. Response.Write "Invalid Email Address.<p>"
  54. Response.Write "</a>"
  55. Response.End
  56. End If
  57.  
  58. If Not (StrComp(objRS("Password"), Request.Form("password"), _
  59. vbBinaryCompare) = 0) Then
  60. objRS.Close
  61. Set objRS = Nothing
  62. oConn.Close
  63. Set oConn = Nothing
  64. Response.Write "<a href='login.html'>"
  65. Response.Write "Invalid password.<p>"
  66. Response.Write "</a>"
  67. Response.End
  68. End If
  69. %>
  70.  
  71. <!-- create form, fill with values from table -->
  72. <form method=post action="modifyuser.asp">
  73. <p>
  74. State: <select name=state type=text value="<%=objRS("State")%>">
  75. <option selected="state">Choose State</option>
  76.     <option value="AL">AL</option>
  77.     <option value="AR">AR</option>
  78.     <option value="AZ">AZ</option>
  79.     <option value="CA">CA</option>
  80.     <option value="CO">CO</option>
  81.     <option value="CT">CT</option>
  82.     <option value="DC">DC</option>
  83. </select>
  84. <p>
  85. <input type=reset> <input type=submit>
  86. </form>
  87. </body>
  88. </html>
  89. <%
  90. objRS.Close
  91. Set objRS = Nothing
  92. oConn.Close
  93. Set oConn = Nothing
  94. %>

In this code, I simply allow the user to edit their "State". When the user registers, they choose a state which is stored in my database. However, when I go to the edituser.asp page, it just shows "Choose State". How do I get the edituser.asp page to show which state they said they were from when they registered?

thanks - Jerry

Newbie
 
Join Date: Nov 2008
Posts: 1
#2: Nov 5 '08

re: How do you show Dropdown menu selected value in edit user page


Use javascript at the bottom of your page, something like this:

<script type="text/javascript">
document.form.state.value = "<%=state%>";

//etc..
</script>
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#3: Nov 5 '08

re: How do you show Dropdown menu selected value in edit user page


Jerry,

Azrar's suggestion works (and might be the best approach), but if you want an ASP solution, it would look something like this:
Expand|Select|Wrap|Line Numbers
  1. <select name=state type=text>
  2. <option>Choose State</option>
  3.     <option value="AL"
  4. <% if objRS("state") = "AL" then response.write "SELECTED" %>
  5. >AL</option>
  6.     <option value="AR"
  7. <% if objRS("state") = "AR" then response.write "SELECTED" %>
  8. >AR</option>
  9.     <option value="AZ"
  10. <% if objRS("state") = "AZ" then response.write "SELECTED" %>
  11. >AZ</option>
  12.     <option value="CA"
  13. <% if objRS("state") = "CA" then response.write "SELECTED" %>
  14. >CA</option>
  15.     <option value="CO"
  16. <% if objRS("state") = "CO" then response.write "SELECTED" %>
  17. >CO</option>
  18.     <option value="CT"
  19. <% if objRS("state") = "CT" then response.write "SELECTED" %>
  20. >CT</option>
  21.     <option value="DC"
  22. <% if objRS("state") = "DC" then response.write "SELECTED" %>
  23. >DC</option>
  24. </select>
Of course if you had the list of states in an array or some other form that you could loop through them, that would make it significantly easier.

Jared
GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 145
#4: Nov 5 '08

re: How do you show Dropdown menu selected value in edit user page


If your option group comes from a database, then you can do as I usually do:

Expand|Select|Wrap|Line Numbers
  1. <select name="txtState">
  2.     <% Do While Not stateRS.EOF
  3.         If stateRs("statename") = userRS("state") Then %>
  4.             <Option selected><%=stateRs("typename")%></option>
  5.         <% Else %>
  6.             <Option><%=CatRs("statename")%></option>
  7.         <% End If 
  8.     stateRs.MoveNext()
  9.     Loop %>
  10. </select>
  11.  
Gaz
Member
 
Join Date: Oct 2008
Location: Cleveland Ohio
Posts: 67
#5: Nov 6 '08

re: How do you show Dropdown menu selected value in edit user page


You guys are the best. Thank you so very much for all of your reponses and help.

I ended up using jhardman's suggestion because it seemed to fit into my page the best. And it works exactly the way I had hoped.

Have a great day.

Jerry
Member
 
Join Date: Oct 2008
Location: Cleveland Ohio
Posts: 67
#6: Nov 6 '08

re: How do you show Dropdown menu selected value in edit user page


Hi, I have a follow up question.

I also have a checkbox section in my edit user information section that I would like to show the user's registration choice on the edituser.asp page.

Currently, it is empty no matter what the user entered during registration

How do I code this so that a check mark will show up if they originally checked the box during registration?

Thanks
GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 145
#7: Nov 6 '08

re: How do you show Dropdown menu selected value in edit user page


Well, Selects have a property called "selected", and check boxes have a similar property called "checked", so therein lies your clue.

Expand|Select|Wrap|Line Numbers
  1. <% If rs("somefield") = true Then %>
  2.     <input type="checkbox" name="chkSubscribed" value="subscribed" checked>
  3. <% Else %>
  4.     <input type="checkbox" name="chkSubscribed" value="subscribed">
  5. <% End If %>

Gaz.
Member
 
Join Date: Oct 2008
Location: Cleveland Ohio
Posts: 67
#8: Nov 6 '08

re: How do you show Dropdown menu selected value in edit user page


Thanks Gaz.

I tried the following code but it isn't working for me. I think I might have the name or value incorrect.

I am using the name of the checkbox -"nonewsemail"- for the input name and left "subscribed" for the value as seen in the code below. Is that correct? If not, what am I supposed to use.

thanks again for all your help, I really appreciate it.

Jerry

Expand|Select|Wrap|Line Numbers
  1. Please check here to opt out of receiving the monthly newsletter:
  2. <% If objRS("nonewsemail") = true Then %>
  3.     <input type="checkbox" name="nonewsemail" value="subscribed" checked />
  4.     <% Else %>
  5.     <input type="checkbox" name="nonewsemail" value="subscribed" />
  6.     <% End If %>Please check here to opt out of receiving the monthly newsletter:
  7. <% If objRS("nonewsemail") = true Then %>
  8.     <input type="checkbox" name="nonewsemail" value="subscribed" checked />
  9.     <% Else %>
  10.     <input type="checkbox" name="nonewsemail" value="subscribed" />
  11.     <% End If %>
GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 145
#9: Nov 6 '08

re: How do you show Dropdown menu selected value in edit user page


I really can't say why that's not working for you (did you mean to paste the code twice?).

I suggest you Response.Write your nonewsemail variable, to see what it contains.
Member
 
Join Date: Oct 2008
Location: Cleveland Ohio
Posts: 67
#10: Nov 7 '08

re: How do you show Dropdown menu selected value in edit user page


I'm sorry about the last post, I accidently put the script in twice. Here is the script I am using. It won't work for some reason.

I am not sure how to do the Response.Write function you speak of.

My form shows up with a checkbox but everytime I try to edit this and include a checkmark in the box, it goes through with the edit but doesn't show the box as checked when I re-visit the edit user page.

Could it possible be the way my access field is set up? I have it set to text and don't required anything to be entered.

Expand|Select|Wrap|Line Numbers
  1. Please check here to opt out of receiving the monthly newsletter:
  2. <% If objRS("nonewsemail") = true Then %>
  3.     <input type="checkbox" name="nonewsemail" value="" checked>
  4.     <% Else %>
  5.     <input type="checkbox" name="nonewsemail" value="">
  6.     <% End If %>
GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 145
#11: Nov 7 '08

re: How do you show Dropdown menu selected value in edit user page


If your field was set up as text that makes sense, what happens if you change the database field to Yes/No type?

BTW, all I meant before was:

Expand|Select|Wrap|Line Numbers
  1. <%
  2. Response.Write "<p>Dubug nonewsemail: " & ObjRs("nonewsemail") & "</p>"
  3. %>
to see what comes from the database.

When putting booleans back in to the database via a checkbox, you should convert the value. I keep this handy:

Expand|Select|Wrap|Line Numbers
  1. function ChkToBool(value)
  2.         if ucase(value)="ON" then
  3.             ChkToBool=true
  4.         else
  5.             ChkToBool=false
  6.         end if
  7.     end function
Which is then used like, for example:

Expand|Select|Wrap|Line Numbers
  1. rs("SomeField")=ChkToBool(request("SomeCheckBox"))
  2. rs.Update
  3.  
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#12: Nov 7 '08

re: How do you show Dropdown menu selected value in edit user page


if your database says
on / off in the column you need to check against "on" or "off" not true/false
if it says
1 / 0 you can test this with true or false
if it says
true / false then you gotta test it with "true" or "false"
GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 145
#13: Nov 7 '08

re: How do you show Dropdown menu selected value in edit user page


Quote:

Originally Posted by iam_clint

if your database says
on / off in the column you need to check against "on" or "off" not true/false
if it says
1 / 0 you can test this with true or false
if it says
true / false then you gotta test it with "true" or "false"

Well, its Access, so yes/no - which compares with true/false. At least it does for me.
Member
 
Join Date: Oct 2008
Location: Cleveland Ohio
Posts: 67
#14: Nov 7 '08

re: How do you show Dropdown menu selected value in edit user page


Thanks for the input.

Well, I got the edituser page to work. I just had to change the field box in my access table to "yes/no" instead of "text".

However, when I do so, my registration.asp page shows an error. When I sumbit the registration, I get an error pointing me to my asp page line that reads

Expand|Select|Wrap|Line Numbers
  1. objRS("NoNewsEmail") = Request.Form("nonewsemail") 
I don't understand why this error occurs once I change the access table field to yes/no. "NoNewsEmail" is the column in my access table, and "nonewsemail" is the input name for the form checkbox section.

Any ideas why this is happening?
GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 145
#15: Nov 8 '08

re: How do you show Dropdown menu selected value in edit user page


I refer you to my post number 11.
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#16: Nov 8 '08

re: How do you show Dropdown menu selected value in edit user page


Quote:

Originally Posted by jerrydigital

Thanks for the input.

Well, I got the edituser page to work. I just had to change the field box in my access table to "yes/no" instead of "text".

However, when I do so, my registration.asp page shows an error. When I sumbit the registration, I get an error pointing me to my asp page line that reads

Expand|Select|Wrap|Line Numbers
  1. objRS("NoNewsEmail") = Request.Form("nonewsemail") 
I don't understand why this error occurs once I change the access table field to yes/no. "NoNewsEmail" is the column in my access table, and "nonewsemail" is the input name for the form checkbox section.

Any ideas why this is happening?

Markrawlingson wrote an article on this subject. It pretty much covers all the issues and how to avoid the problem.

Jared
Member
 
Join Date: Oct 2008
Location: Cleveland Ohio
Posts: 67
#17: Nov 9 '08

re: How do you show Dropdown menu selected value in edit user page


Thank you all for your great explanations and help.

I ended up just using a drop down menu where the user enters yes or no. I took the easy way out and I am not pleased with that but I am under time constraints. Hopefully I will get some time in the future to revisit this question and put all of your tips to use. Thanks again for your help on this matter, I truely appreciate all of it.

Jerry
Newbie
 
Join Date: Jul 2008
Posts: 30
#18: Nov 11 '08

re: How do you show Dropdown menu selected value in edit user page


This post is helpful. I had the same problem and now solved.
Reply