Forum Moderators: open
Is there any way to pull this off? Thanks!
<select name="contact" id="contact">
<option value="">Select Contact</option>
<option value="1">John Doe</option>
<option value="2">Jane Doe</option>
<option value="3">John Smith</option>
</select>
Then your server-side processor would do
1=johndoe@example.com
2=janedoe@example.com
3=johnsmith@example.com
And the submittor never sees the email address, which is the largest inception point for spam abuse.
But if you must, something like this should work. :-)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<!-- doctype all on one line! -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
function popField() {
//Always IF documentGetElementById. So much easier than checking browsers.
if (document.getElementById('to_email')) {
var ind = document.getElementById('contact').selectedIndex;
if (ind == 0) { document.getElementById('to_email').value=''; }
else { document.getElementById('to_email').value=
document.getElementById('contact').options[ind].value;
// Put the previous two lines all on one line,
//it was making page too wide here.
}
}
}
</script>
</head>
<body>
<form>
<select name="contact" id="contact" onChange="popField();">
<option value="">Select Contact</option>
<option value="johndoe@example.com">John Doe</option>
<option value="janedoe@example.com">Jane Doe</option>
<option value="johnsmith@example.com">John Smith</option>
</select>
<input type="text" name="to_email" id="to_email">
</form>
</body>
</html>
Please note the use of a blank select list item. This is what allows you to reset the list.
For JS, you could use the text attribute of the option:
function popField() {
if (document.getElementById('to_email')) {
var ind = document.getElementById('contact').selectedIndex;
if (ind == 0) { document.getElementById('to_email').value=''; }
else {
document.getElementById('to_name').value=
document.getElementById('contact').options[ind].text;
document.getElementById('to_email').value=
document.getElementById('contact').options[ind].value;
}
}
}
Alternatively you could separate the values in the select with a colon
<option value="John Doe:johndoe@example.com">John Doe</option>
And use the split() method
else {
var vals = document.getElementById('contact').options[ind].value.split(':');
document.getElementById('to_name').value= vals[0];
document.getElementById('to_email').value= vals[1];
}
Both assuming you add a field id'ed "to_name".
*****************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <!-- doctype all on one line! --> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
function popField() { if (document.getElementById('to_email')) { var ind = document.getElementById('contact').selectedIndex; if (ind == 0) { document.getElementById('to_email').value=''; } else { document.getElementById('to_name').value= document.getElementById('contact').options[ind].text; document.getElementById('to_email').value= document.getElementById('contact').options[ind].value; } } } </script>
</head>
<body> <form> <select name="contact" id="contact" onChange="popField();"> <option value="">Select Contact</option> <option value="johndoe@example.com">John Doe</option> <option value="janedoe@example.com">Jane Doe</option> <option value="johnsmith@example.com">John Smith</option> </select> <input type="text" name="to_email" id="to_email"> </form><P></P> </body> </html>
********************