Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Obtaining Label or Name from a FORM--not just value.

Posted on 2003-12-05
4
Medium Priority
?
190 Views
Last Modified: 2010-04-09
A drop down menu list:

<option value="82347982374">DrugA</option>
<option value="82323432374">DrugB</option>
<option value="823wererrt34">DrugC</option>
<option value="82asf82374">DrugD</option>
<option value="8234sfsfft74">DrugE</option>
<option value="8234235d74">DrugF</option>

After choose this drop down, it'll submit to an action page and I like to say: You chose, among other things being displayed, Drug F.

How is this accomplish?
0
Comment
Question by:Danniken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 11

Accepted Solution

by:
Zontar earned 100 total points
ID: 9886573
Only the value of the option is passed, not the text. Exception: if there's no value attribute* for an option, then its text is passed instead.

If you want something else displayed other than the value, you'll have to code it using an array/hash/other data structure or retrieve it from a database.

Another solution is to use JavaScript to get the text of the selected option and store it in a hidden form field prior to submission:

formObj.hiddenField.value = formObj.selectName.options[formObj.selectName.selectedIndex].text;

---------------------------------------

*That is, if the value attribute's not present at all; value="" will pass an empty string.
0
 
LVL 16

Assisted Solution

by:jaysolomon
jaysolomon earned 100 total points
ID: 9887037
here is an example of getting the selectedIndex text and value at the same time.

I made it alert on purpose, so you could see. You would want to change the doIt(selObj) function so that it will pass the value and or the text to the next page. Or if its a pop up window you can use document.write("You Chose: "+ getSelectText(opener.document.forms[0].sel1 +"   The value is "+ getSelectValue(opener.document.forms[0].sel1));

code

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--Begin
function getSelectText(selObj)
{
      return selObj.options[selObj.selectedIndex].text;
}
function getSelectValue(selObj)
{
      return selObj.options[selObj.selectedIndex].value;
}
function doIt(selObj)
{
      alert("You Chose: "+ getSelectText(selObj) +"\n\nValue = "+ getSelectValue(selObj));
}
//End-->
</script>
</head>
<body>
<form name="form1" method="post" action="">
<select name="sel1" onchange="doIt(this);">
<option>Select-One</option>
<option value="82347982374">DrugA</option>
<option value="82323432374">DrugB</option>
<option value="823wererrt34">DrugC</option>
<option value="82asf82374">DrugD</option>
<option value="8234sfsfft74">DrugE</option>
<option value="8234235d74">DrugF</option>
</select>
</form>
</body>
</html>
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question