I have limited experience with JSP, but I think the problem is the same here for any server-side script. When the form is submitted the text between the option tag itself isn't submitted if the value attribute is present (as far as I know anyway). If there is some way to access the text value server-side when the value attribute exists someone please correct me.
I'm assuming you want to put a record into your database and send the user some sort of confirmation email, with the code going into the db and the text going into the email. If that's the case then I think you could add a hidden field to the form and use client-side javascript to set its value when the user makes a choice from the list box - using the text inside the option instead of the code inside the value attribute.
Then when the form is submitted you could retrieve the value of the list box and enter it into your db as you're already doing, but when you go to send the email you could retrieve the value of the hidden field containing the text of the selection and send that instead. I can't show you an example in JSP, but the javascript code should work regardless of the server-side language. This is how I threw it together with PHP:
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
<!--
function setHiddenField()
{
var f = document.forms['nameOfYourForm'];
var hidden = f.elements['property_desc'];
var i = f.elements['property_code'].selectedIndex
var textVal = f.elements['property_code'].options[i].text;
hidden.value = textVal;
}
// -->
</script>
</head>
<body>
<form name="nameOfYourForm" action="<?=$PHP_SELF ?>" method="post">
<select name="property_code" onchange="setHiddenField()">
<option value="">-----Select-----</option>
<option value="AP">Apartment</option>
<option value="CD">Condo</option>
<option value="LF">Loft</option>
</select>
<input type="hidden" name="property_desc" value="">
<br><br>
<input type="submit" />
</form>
<?
$codeVal = $_POST['property_code'];
$descVal = $_POST['property_desc'];
echo "code value = " . $codeVal . "<br>";
echo "desc value = " . $descVal . "<br>";
?>
</body>
</html>
- <html>
- <head>
- <title>Untitled</title>
- <script type="text/javascript">
- <!--
- function setHiddenField()
- {
- var f = document.forms['nameOfYourForm'];
- var hidden = f.elements['property_desc'];
- var i = f.elements['property_code'].selectedIndex
- var textVal = f.elements['property_code'].options[i].text;
- hidden.value = textVal;
- }
- // -->
- </script>
- </head>
- <body>
- <form name="nameOfYourForm" action="<?=$PHP_SELF ?>" method="post">
- <select name="property_code" onchange="setHiddenField()">
- <option value="">-----Select-----</option>
- <option value="AP">Apartment</option>
- <option value="CD">Condo</option>
- <option value="LF">Loft</option>
- </select>
- <input type="hidden" name="property_desc" value="">
- <br><br>
- <input type="submit" />
- </form>
- <?
- $codeVal = $_POST['property_code'];
- $descVal = $_POST['property_desc'];
- echo "code value = " . $codeVal . "<br>";
- echo "desc value = " . $descVal . "<br>";
- ?>
- </body>
- </html>
I tested the client-side code in Netscape 4.08, FireFox 0.8, IE 6 and Opera 7. Obviously it won't work for the small number of people who have javascript disabled, so you might want to check to see whether the hidden field is just an empty string, in which case I think your only option would be to substitute the code from the list box.
Here's a test page of the PHP example:
http://www.gotrivia.com/testing/test1.php
Free Programming Resources