Connecting Tech Pros Worldwide Help | Site Map

Display description in dropdown list but store another value in variable

Member
 
Join Date: May 2008
Posts: 43
#1: Sep 19 '08
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?

Expand|Select|Wrap|Line Numbers
  1. <select name="ReportName" id="ReportName">  
  2. <option><%=strReportName%></option>  
  3. <option value="Rpt0001">Report A</option>
  4. <option value="Rep0002">Report B</option>
  5. <option value="Rep0003">Report C</option>
  6. <option value="Rep0004">Report D</option>
  7. </select>
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#2: Sep 19 '08

re: Display description in dropdown list but store another value in variable


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

Expand|Select|Wrap|Line Numbers
  1. <%
  2. strReportName = Request("ReportName")
  3. %>
If that doesn't help could you try to explain the problem again?

Thanks,

Dr B
jeffstl's Avatar
Expert
 
Join Date: Feb 2008
Posts: 413
#3: Sep 19 '08

re: Display description in dropdown list but store another value in variable


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

Expand|Select|Wrap|Line Numbers
  1. strReportName = request.form("ReportName")
  2.  
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.
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#4: Sep 22 '08

re: Display description in dropdown list but store another value in variable


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:
Expand|Select|Wrap|Line Numbers
  1. <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:
Expand|Select|Wrap|Line Numbers
  1. <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
Member
 
Join Date: May 2008
Posts: 43
#5: Sep 23 '08

re: Display description in dropdown list but store another value in variable


Quote:

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

Expand|Select|Wrap|Line Numbers
  1. <%
  2. strReportName = Request("ReportName")
  3. %>
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
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#6: Sep 24 '08

re: Display description in dropdown list but store another value in variable


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
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#7: Sep 24 '08

re: Display description in dropdown list but store another value in variable


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:

Expand|Select|Wrap|Line Numbers
  1. <form name='f1'>
  2. <select name='s1'>
  3.     <option value='001£John Smith'>John Smith</option>
  4.     <option value='002£Paul Jones>Paul Jones</option>
  5. </select>
  6. </form>
  7.  
  8. <%
  9. Dim s1, strValue, strDescription
  10. s1 = Request("s1")
  11. If InStr(s1, "£") > 0 Then
  12.     strValue = Split(s1, "£")(0)
  13.     strDescription= Split(s1, "£")(1)
  14. End If
  15. Response.Write "Value is " & strValue
  16. Response.Write "<br />"
  17. Response.Write "Description is " & strDescription
  18. %>
Does this make sense? Let me know how you get on.

Dr B
Member
 
Join Date: May 2008
Posts: 43
#8: Sep 25 '08

re: Display description in dropdown list but store another value in variable


Quote:

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:

Expand|Select|Wrap|Line Numbers
  1. <form name='f1'>
  2. <select name='s1'>
  3.     <option value='001£John Smith'>John Smith</option>
  4.     <option value='002£Paul Jones>Paul Jones</option>
  5. </select>
  6. </form>
  7.  
  8. <%
  9. Dim s1, strValue, strDescription
  10. s1 = Request("s1")
  11. If InStr(s1, "£") > 0 Then
  12.     strValue = Split(s1, "£")(0)
  13.     strDescription= Split(s1, "£")(1)
  14. End If
  15. Response.Write "Value is " & strValue
  16. Response.Write "<br />"
  17. Response.Write "Description is " & strDescription
  18. %>
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.
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#9: Sep 25 '08

re: Display description in dropdown list but store another value in variable


No problem, glad to help.

Dr B
Reply