Display description in dropdown list but store another value in variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rmurgia
    New Member
    • May 2008
    • 63

    Display description in dropdown list but store another value in variable

    Is there a way to set up a dropdown list and have the description field be displayed as a selection for the user and also be displayed in the input field after the user selects it, but have the value be stored in the variable? In the example below, the user would see Report A, but Rpt0001 would be stored in variable strReportName. Is it possible to do this?

    Code:
    <select name="ReportName" id="ReportName">  
    <option><%=strReportName%></option>  
    <option value="Rpt0001">Report A</option>
    <option value="Rep0002">Report B</option>
    <option value="Rep0003">Report C</option>
    <option value="Rep0004">Report D</option>
    </select>
    Last edited by DrBunchman; Sep 19 '08, 07:07 AM. Reason: Added [Code] Tags - Please use the '#' button
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi rmurgia,

    I'm not 100% clear on what you are trying to do. If you want to assign the value of the drop down to the variable strReportName then you can do so after your form has been submitted with

    Code:
    <%
    strReportName = Request("ReportName")
    %>
    If that doesn't help could you try to explain the problem again?

    Thanks,

    Dr B

    Comment

    • jeffstl
      Recognized Expert Contributor
      • Feb 2008
      • 432

      #3
      You will not store the value of the selected option in strReportName based on the code you posted no.

      You would need to catch the value on the request.form side after the form is submitted to a .asp as a post.

      Similar to how Dr. B put it

      Code:
      strReportName = request.form("ReportName")
      So yes it is possible to answer your question. Is this what you were asking?

      What you posted would create a drop down with the top choice a blank one because you have that choice set to strReportName which at the time of running I assume has nothing in it...therefore blank option in your drop down. So probably not what your going for.

      Comment

      • jhardman
        Recognized Expert Specialist
        • Jan 2007
        • 3405

        #4
        I guess I'm also not sure what he's asking, but it looks to me like the answer is a simple "yes". You know that if you just right this:
        Code:
        <option>report a</option>
        then you see "report a" in the drop down and this value is passed when the form is submitted. Many people code like this. On the other hand, it would be better to code like this:
        Code:
        <option value="rpt_a">report a</option>
        so that the user sees "report a" but the text "rpt_a" is sent to the form handler. It sure sounds to me that this is all he's asking - and the answer is "yes".

        Jared

        Comment

        • rmurgia
          New Member
          • May 2008
          • 63

          #5
          Originally posted by DrBunchman
          Hi rmurgia,

          I'm not 100% clear on what you are trying to do. If you want to assign the value of the drop down to the variable strReportName then you can do so after your form has been submitted with

          Code:
          <%
          strReportName = Request("ReportName")
          %>
          If that doesn't help could you try to explain the problem again?

          Thanks,

          Dr B

          Dr B,

          Thanks for the reply. I guess I was not clear enough in my example. What I was trying to do was to show a dropdown which displays some information to the user, but stores different information in the variable. The current situation is where the user sees and employee # and employee name on the dropdown:

          Emp # Emp Name
          001 John Smith
          002 Paul Jones

          After the name Paul Jones is selected, I wanted to display Paul Jones in the field, but store 002 in the variable. Currently after the selection is made, the user sees 002 in the field and the name is no longer available:

          Emp: 001

          It seems that the only way to accomplish this would be to create another variable to hold the name and then display it as a non-updatable field after the selection is made:

          Emp #: 001
          Emp Name: Paul Jones

          Comment

          • jhardman
            Recognized Expert Specialist
            • Jan 2007
            • 3405

            #6
            I think I've got you. Once the form is submitted, the number is submitted rather than the name, so for example on a confirmation page the user sees a number which means nothing outside of the correct context. Is this right? If so, then let me ask you how you have the data stored (are the employee name and numbers coming from a db?) - the solution depends on where the data comes from.

            A long time ago I made a non-compliant javascript function that added an unsupported attribute to the options, then I got the extra attributes value to pass to a hidden text field for similar reasons to yours. Although I got this to work in IE5, there are better solutions around today. Anyway, answer my question, and I'll tell you what I got.

            Jared

            Comment

            • DrBunchman
              Recognized Expert Contributor
              • Jan 2008
              • 979

              #7
              You could put the description alongside the value in the value attribute of your options and separate them with a delimiter. Then parse that requested value after the form is submitted. Take a look at this example:

              Code:
              <form name='f1'>
              <select name='s1'>
                  <option value='001£John Smith'>John Smith</option>
                  <option value='002£Paul Jones>Paul Jones</option>
              </select>
              </form>
              
              <%
              Dim s1, strValue, strDescription
              s1 = Request("s1")
              If InStr(s1, "£") > 0 Then
                  strValue = Split(s1, "£")(0)
                  strDescription= Split(s1, "£")(1)
              End If
              Response.Write "Value is " & strValue
              Response.Write "<br />"
              Response.Write "Description is " & strDescription
              %>
              Does this make sense? Let me know how you get on.

              Dr B

              Comment

              • rmurgia
                New Member
                • May 2008
                • 63

                #8
                Originally posted by DrBunchman
                You could put the description alongside the value in the value attribute of your options and separate them with a delimiter. Then parse that requested value after the form is submitted. Take a look at this example:

                Code:
                <form name='f1'>
                <select name='s1'>
                    <option value='001£John Smith'>John Smith</option>
                    <option value='002£Paul Jones>Paul Jones</option>
                </select>
                </form>
                
                <%
                Dim s1, strValue, strDescription
                s1 = Request("s1")
                If InStr(s1, "£") > 0 Then
                    strValue = Split(s1, "£")(0)
                    strDescription= Split(s1, "£")(1)
                End If
                Response.Write "Value is " & strValue
                Response.Write "<br />"
                Response.Write "Description is " & strDescription
                %>
                Does this make sense? Let me know how you get on.

                Dr B
                Dr B,

                You have hit on exactly what I was describing. I wanted to display a meaningful name to the user, but store the key (in this case the employee Id) in the field. In the process of wanting a fast solution, I made another call to the database to retrieve the employee name using the employee Id. However, I like your solution better, since I can avoid the second call to the database.

                Thanks for the help.

                Comment

                • DrBunchman
                  Recognized Expert Contributor
                  • Jan 2008
                  • 979

                  #9
                  No problem, glad to help.

                  Dr B

                  Comment

                  Working...