Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 14th, 2009, 03:28 AM
Authorized User
 
Join Date: Feb 2006
Location: jaipur, rajasthan, India.
Posts: 67
Thanks: 7
Thanked 0 Times in 0 Posts
Send a message via Yahoo to beetle_jaipur
Default Problem with Select Tag

Hi,

i have a tiny problem with select tag

i have a select tag like this

HTML Code:
<select name="checkDropDown" onchange="elements['TextwithList'].value = this.text">
         <option value="1">ashok</option>
         <option value="2">amit</option>
         <option value="3">Sunil</Option>
</select>
and i want to store both those values into database
1. value(1, 2...)
2. Showed text (ashok, ....)

how to get both values

i tried this code
<%
dim value
dim SelectText

value = Request.Form("CheckDropDown")
SelectText =Request.Form("checkDropDown").text
...
...
...
%>

cant understand how to select this value, i tried to find on internet, but could not succeed

all suggestions are welcome,
thanks for your consideration

best regards

ashok sharma
Reply With Quote
  #2 (permalink)  
Old March 14th, 2009, 01:38 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default Use a Dictionary to show and retrieve values

Hi Ashok,
There isn't a way to get the text value they selected just by using Request.Form - you only ever get back the value part (you may be thinking of how ASP.NET allows you to use it, but that does it by parsing the html code in the file - nothing to do with request data).

You will have to keep a list of the text and values you want to show within the actual asp code.
A good way to do this is using an ASP Dictionary object, which is like an array, but stored as custom keys/values. See an introduction at http://www.w3schools.com/asp/asp_ref_dictionary.asp

Here is an example page which uses a Dictionary to print and then retrieve the text and value they have selected

vb Code:
<%

Dim myDic, dKeys, text, value

' create a dictionary object
Set myDic = Server.CreateObject("Scripting.Dictionary")
' add each value/text pair
myDic.Add "1", "ashok"
myDic.Add "2", "amit"
myDic.Add "3", "Sumil"

' store the list of keys (select values)
dKeys = myDic.Keys()

If Request.Form("checkDropDown") <> "" Then
  ' get the value selected and use dictionary to get text
  value = Request.Form("checkDropDown")
  text = myDic(value)
  ' add to database here
End If
%>
<html>
<head>
</head>

<body>
<form action="default.asp" method="post">

<select name="checkDropDown">
<%
' loop through dictionary, printing an option containing each key/value
Dim optValue, optText
For i = 0 To myDic.Count - 1
    optValue = dKeys(i)
    optText = myDic(optValue)
    Response.Write "<option value=""" & optValue & """>" & optText & "</option>"
Next
%>
</select>

<input type="submit" value="Go" />

</form>

<%
' For testing only
If Not IsEmpty(text) Then
    Response.Write text & " / " & value
End If
%>
</body>
</html>

You basically create the dictionary at the top of the page each time. If they have submitted the form, it will retrieve the selected value from the Request, then use the Dictionary to find the corresponding text. In the same way, it builds up the select html using the keys and values from the Dictionary.

HTH
Phil

Last edited by philip_cole; March 14th, 2009 at 01:39 PM.. Reason: Typing errors
Reply With Quote
The Following User Says Thank You to philip_cole For This Useful Post:
beetle_jaipur (March 28th, 2009)
  #3 (permalink)  
Old March 15th, 2009, 06:29 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

A very strange requirement, i cant imagine why you would wany the text value also. Shouldnt your select list be created from a DB table, all your should need is the ID, then you would get the string based on that ID from the table.......

Not that I have done this before but I can see a much less complicated way. There is no need to store all your values in an array - Im mean after all you only want the two values that are being selected correct? This is a bit of a hack but much much less code than whats been suggested.

Place both values inside the value="" seperated by a comer like so

<select name="checkDropDown" onchange="elements['TextwithList'].value = this.text">
<option value="1,ashok">ashok</option>
<option value="2,amit">amit</option>
<option value="3,Sunil">Sunil</Option>
</select>

Now your trim(request.form("checkDropDown")) will return:

1,ashok

if the first option is selected. Now use the Split function, split on the commer and there are your two values. As I said a hack but a simple one that will work for you. Sure you may have to alter your onChange code but that should be strait forward.
__________________
Wind is your friend
Matt
Reply With Quote
The Following User Says Thank You to mat41 For This Useful Post:
beetle_jaipur (March 28th, 2009)
  #4 (permalink)  
Old March 16th, 2009, 04:23 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default And yet another way to do it...

Code:
<select name="checkDropDown"
     onchange="this.form.checkText.value=this.options[this.selectedIndex].text">
<option value="1">ashok</option>
<option value="2">amit</option>
<option value="3">Sunil</Option>
 </select>
<input type=hidden name="checkText">

...

I think this may have been what Ashok was trying to do in the first place.

Now, in your ASP code:
Code:
dropDownValue = Request("checkDropDown")
dropDownText  = Request("checkText")
Personally, I usually use Mat's method, but there are times when doing it this way is handier. (Only thing I would change in Mat's code: Don't use a comma in the value="1,ashok" ... use some other delimiter, maybe semicolon or $ ... in case you want to use <SELECT MULTIPLE> ... simplifies coding for multiple values later. Not really important if you'll never use MULTIPLE.)




Last edited by Old Pedant; March 16th, 2009 at 04:26 AM..
Reply With Quote
The Following User Says Thank You to Old Pedant For This Useful Post:
beetle_jaipur (March 28th, 2009)
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to select only the parent tag and ignore the c Prem Rajesh Kumar XSLT 3 November 6th, 2007 12:53 PM
Quest: How to select the second tag?! gnesland XSLT 4 November 28th, 2006 11:09 AM
get displayed name of the <select> tag nikeshkm HTML Code Clinic 1 October 20th, 2006 11:21 PM
can't read value from select tag using javascript kumar_kumar Javascript 2 February 9th, 2005 11:18 PM
Dynamic Select Tag fs22 Javascript How-To 1 December 15th, 2004 08:32 PM



All times are GMT -4. The time now is 12:04 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.