PDA

View Full Version : n00b needs help



eager-noob
02-09-2007, 12:36 AM
I'm new to Javascript and can make sense of some - but I have a task which I cannot figure out how to do. Just learning - and enjoying it.

If anyone can impart some wisdom - I'd appreciate it.

I have a form which only validates the fields for being poulated.
What I WANT to do is to populate a hidden field, based on a drop-down field.

So - you select a category (let's say 1- 4).
The Value of this field is passed to an Email handler.
I want to change the Mailto value - based on the category value.

How do I do it (at least conceptually).???

mburt
02-09-2007, 12:48 AM
I'm not really sure what you mean by "Mailto" value, but try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function changeVal(elem) {
document.getElementById("data").value = elem.options[elem.selectedIndex].value
}
</script>
</head>
<body>
<select onchange="changeVal(this)">
<option value="value 1">My Option 1</option>
<option value="value 2">My Option 2</option>
<option value="value 3">My Option 3</option>
<option value="value 4">My Option 4</option>
</select>
<br><input id="data" type="hidden">
</body>
</html>
That gets your values for corresponding selected options.
Remove the type="hidden" part if you want to see the result.

mburt
02-09-2007, 12:56 AM
Or maybe something more similar to:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
var sent = "Send mail to: "
function changeVal(elem) {
var mailto = document.getElementById("mailer")
mailto.href = "mailto:"+elem.options[elem.selectedIndex].value
var txt = document.createTextNode(sent+elem.options[elem.selectedIndex].firstChild.nodeValue)
while (mailto.firstChild) {
mailto.removeChild(mailto.firstChild)
}
mailto.appendChild(txt)
}
</script>
</head>
<body>
<select onchange="changeVal(this)">
<option value="mymail@mail.com">My E-mail</option>
<option value="value2">My Option 2</option>
<option value="value3">My Option 3</option>
<option value="value4">My Option 4</option>
</select>
<br><a href="#" id="mailer"></a>
</body>
</html>
I'm still not sure what you're asking.

eager-noob
02-09-2007, 01:07 AM
Thanks Mike.

The form passes to a CDONTS object.
The mailto is just a hidden field within the form:


<input type="hidden" name="mailto" value="me@mysite.com>

If someone selects category 1 - I want to send it to adam@mysite.com
If someone selects category 2 - I want to send it to marcus@mysite.com

Does that explain it?

Twey
02-09-2007, 01:17 AM
<script type="text/javascript">
var addresses = [
"adam@mysite.com",
"marcus@mysite.com"
];

function updateMail(frm, val) {
frm.elements.mailto = addresses[val];
}
</script>
<select onchange="updateMail(this.form, this.selectedIndex);" name="category">
<option value="1">Category 1</option>
<option value="2">Category 2</option>
</select>However, a server-side solution would definitely be preferable -- it would neither break the script for non-JS users nor reveal the email addresses to potential spambots.

eager-noob
02-09-2007, 01:29 AM
Hey Twey - that's very tight code.
It looks like it's bang on too.

I'll give that a go.

Much appreciation! I'll report back soon.

eager-noob
02-09-2007, 01:44 AM
Hmmm. It doesn't seem to be populating the mailto field.

Yes - I understand what you mean about server-side. This is more of a learning exercise for me though.
I'll have to go over the code again to absorb it.

mburt
02-09-2007, 01:48 AM
What is the "mailto field" ?

eager-noob
02-09-2007, 01:50 AM
The mailto is an input field within the form.

mburt
02-09-2007, 01:52 AM
Twey didn't specify an attribute to apply the value to... try replacing this line:

function updateMail(frm, val) {
frm.elements.mailto = addresses[val];
}

with


frm.elements.mailto.value = addresses;[val]

eager-noob
02-09-2007, 02:07 AM
Yep - got that.
Did you mean to move the ";" ?

eager-noob
02-09-2007, 02:12 AM
Your placement of the semi-colon confused me.
I tried it anyway, and it added the whole string - and sent an email to each address.
I'm not really following how this code picks out which value is actually selected.

Still no result with the semi-colon at the end of the line:

function updateMail(frm, val) {
frm.elements.mailto.value = addresses[val];
}

eager-noob
02-09-2007, 03:00 AM
Thanks Twey and Mike.
Much appreciated.

Am looking forward to absorbing that - and learning more from this forum.

Mighty fine!

Shotgun Ninja
02-09-2007, 02:54 PM
Hey, could you guys help me with MY e-mail form?



...
/* User's E-Mail address (entered by user in public-accessible form) */
<input type="text" name="REPLY"><br>
/* Requested folder name (for private subweb creation)*/
<input type="text" name="NAME"><br>
/* Requested style selection */
<select name="STYLE">
<option selected>Other (see below)</option>
<option>White</option>
<option>Black</option>
/* Et cetera... */
</select><br>
/* Written specifications for style (specific colors, background pictures, etc.) */
<textarea name="OTHER">
</textarea>
/* Hidden fields for receiving e-mail address, subject line, etc. */
<input type="hidden" name="EMAIL" value="XXXXX@XXXXXX.com">
<input type="hidden" name="SUBJECT" value="Concerning Private Subweb Registration">
...



And that has to go to the email address shown in the hidden value 'EMAIL'.