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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jerrydigital
    New Member
    • Oct 2008
    • 67

    #1

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

    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

    Code:
    <%@ Language=VBScript %>
    <% Option Explicit %>
    <!--#include virtual="/adovbs.inc"-->
    <html>
    <body>
    <%
    Dim oConn, oRs
    Dim connectstr, sDSNDir, tablename
    Dim db_name, db_username, db_userpassword
    Dim dsn_name
    
    dsn_name = "register.dsn"
    tablename = "tblRegister"
    db_username = "****"
    db_userpassword = "****"
    
    sDSNDir = Server.MapPath("/_dsn")
    connectstr = "filedsn=" & sDSNDir & "/" & dsn_name
    
    Set oConn = Server.CreateObject("ADODB.Connection")
    oConn.Open connectstr
    
    Dim objRS, bolFound, strEmail
    strEmail = Request.Form("email")
    
    If strEmail = "" Then
    oConn.Close
    Set objConn = Nothing
    Response.Write "<a href='login.html'>"
    Response.Write "You must enter a email address"
    Response.Write "</a>"
    Response.End
    End If
    
    Set objRS = Server.CreateObject("ADODB.Recordset")
    objRS.Open "tblRegister", oConn, , , adCmdTable
    bolFound = False
    
    Do While Not (objRS.EOF OR bolFound)
    If (StrComp(objRS("Email"), strEmail, vbTextCompare) = 0) Then
    BolFound = True
    Else
    objRS.MoveNext
    End If
    Loop
    
    If Not bolFound Then
    objRS.Close
    Set objRS = Nothing
    oConn.Close
    Set oConn = Nothing
    Response.Write "<a href='login.html'>"
    Response.Write "Invalid Email Address.<p>"
    Response.Write "</a>"
    Response.End
    End If
    
    If Not (StrComp(objRS("Password"), Request.Form("password"), _
    vbBinaryCompare) = 0) Then
    objRS.Close
    Set objRS = Nothing
    oConn.Close
    Set oConn = Nothing
    Response.Write "<a href='login.html'>"
    Response.Write "Invalid password.<p>"
    Response.Write "</a>"
    Response.End
    End If
    %>
    
    <!-- create form, fill with values from table -->
    <form method=post action="modifyuser.asp">
    <p>
    State: <select name=state type=text value="<%=objRS("State")%>">
    <option selected="state">Choose State</option>
    	<option value="AL">AL</option>
    	<option value="AR">AR</option>
    	<option value="AZ">AZ</option>
    	<option value="CA">CA</option>
    	<option value="CO">CO</option>
    	<option value="CT">CT</option>
    	<option value="DC">DC</option>
    </select>
    <p>
    <input type=reset> <input type=submit>
    </form>
    </body>
    </html>
    <%
    objRS.Close
    Set objRS = Nothing
    oConn.Close
    Set oConn = Nothing
    %>

    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
  • azrar
    New Member
    • Nov 2008
    • 11

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

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

    //etc..
    </script>

    Comment

    • jhardman
      Recognized Expert Specialist
      • Jan 2007
      • 3405

      #3
      Jerry,

      Azrar's suggestion works (and might be the best approach), but if you want an ASP solution, it would look something like this:
      Code:
      <select name=state type=text>
      <option>Choose State</option>
      	<option value="AL"
      <% if objRS("state") = "AL" then response.write "SELECTED" %>
      >AL</option>
      	<option value="AR"
      <% if objRS("state") = "AR" then response.write "SELECTED" %>
      >AR</option>
      	<option value="AZ"
      <% if objRS("state") = "AZ" then response.write "SELECTED" %>
      >AZ</option>
      	<option value="CA"
      <% if objRS("state") = "CA" then response.write "SELECTED" %>
      >CA</option>
      	<option value="CO"
      <% if objRS("state") = "CO" then response.write "SELECTED" %>
      >CO</option>
      	<option value="CT"
      <% if objRS("state") = "CT" then response.write "SELECTED" %>
      >CT</option>
      	<option value="DC"
      <% if objRS("state") = "DC" then response.write "SELECTED" %>
      >DC</option>
      </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

      Comment

      • GazMathias
        Recognized Expert New Member
        • Oct 2008
        • 228

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

        Code:
        <select name="txtState">
        	<% Do While Not stateRS.EOF
        		If stateRs("statename") = userRS("state") Then %>
        			<Option selected><%=stateRs("typename")%></option>
        		<% Else %>
        			<Option><%=CatRs("statename")%></option>
        		<% End If 
        	stateRs.MoveNext()
        	Loop %>
        </select>
        Gaz

        Comment

        • jerrydigital
          New Member
          • Oct 2008
          • 67

          #5
          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

          Comment

          • jerrydigital
            New Member
            • Oct 2008
            • 67

            #6
            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

            Comment

            • GazMathias
              Recognized Expert New Member
              • Oct 2008
              • 228

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

              Code:
              <% If rs("somefield") = true Then %>
                  <input type="checkbox" name="chkSubscribed" value="subscribed" checked>
              <% Else %>
                  <input type="checkbox" name="chkSubscribed" value="subscribed">
              <% End If %>

              Gaz.

              Comment

              • jerrydigital
                New Member
                • Oct 2008
                • 67

                #8
                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 -"nonewsemai l"- 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

                Code:
                Please check here to opt out of receiving the monthly newsletter:
                <% If objRS("nonewsemail") = true Then %>
                	<input type="checkbox" name="nonewsemail" value="subscribed" checked />
                	<% Else %>
                	<input type="checkbox" name="nonewsemail" value="subscribed" />
                	<% End If %>Please check here to opt out of receiving the monthly newsletter:
                <% If objRS("nonewsemail") = true Then %>
                	<input type="checkbox" name="nonewsemail" value="subscribed" checked />
                	<% Else %>
                	<input type="checkbox" name="nonewsemail" value="subscribed" />
                	<% End If %>

                Comment

                • GazMathias
                  Recognized Expert New Member
                  • Oct 2008
                  • 228

                  #9
                  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.

                  Comment

                  • jerrydigital
                    New Member
                    • Oct 2008
                    • 67

                    #10
                    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.

                    Code:
                    Please check here to opt out of receiving the monthly newsletter:
                    <% If objRS("nonewsemail") = true Then %>
                    	<input type="checkbox" name="nonewsemail" value="" checked>
                    	<% Else %>
                    	<input type="checkbox" name="nonewsemail" value="">
                    	<% End If %>

                    Comment

                    • GazMathias
                      Recognized Expert New Member
                      • Oct 2008
                      • 228

                      #11
                      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:

                      Code:
                      <%
                      Response.Write "<p>Dubug nonewsemail: " & ObjRs("nonewsemail") & "</p>"
                      %>
                      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:

                      Code:
                      function ChkToBool(value)
                      		if ucase(value)="ON" then
                      			ChkToBool=true
                      		else
                      			ChkToBool=false
                      		end if
                      	end function
                      Which is then used like, for example:

                      Code:
                      rs("SomeField")=ChkToBool(request("SomeCheckBox"))
                      rs.Update

                      Comment

                      • iam_clint
                        Recognized Expert Top Contributor
                        • Jul 2006
                        • 1207

                        #12
                        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"

                        Comment

                        • GazMathias
                          Recognized Expert New Member
                          • Oct 2008
                          • 228

                          #13
                          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.

                          Comment

                          • jerrydigital
                            New Member
                            • Oct 2008
                            • 67

                            #14
                            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.as p page shows an error. When I sumbit the registration, I get an error pointing me to my asp page line that reads

                            Code:
                            objRS("NoNewsEmail") = Request.Form("nonewsemail")
                            I don't understand why this error occurs once I change the access table field to yes/no. "NoNewsEmai l" is the column in my access table, and "nonewsemai l" is the input name for the form checkbox section.

                            Any ideas why this is happening?

                            Comment

                            • GazMathias
                              Recognized Expert New Member
                              • Oct 2008
                              • 228

                              #15
                              I refer you to my post number 11.

                              Comment

                              Working...