if then to change a parameter in javascript

T

TeknoShock

The following information is passed to a script to generate an email
from a form.
<INPUT TYPE="hidden" name="param_recipient" value="(e-mail address removed)">

In the form further down on the page, a user enters a value in the ru
field. I want to write something similar to:
if ru=122 then sendformto="(e-mail address removed)"
if ru=144 then sendformto="(e-mail address removed)"

the paramater above would then become something like:
<INPUT TYPE="hidden" name="param_recipient" value=sendformto>

How would I go about doing this?
 
J

Joseph Taylor

There are many ways to go about doing this, but here's what I'd do.

I'd give the form an onsubmit event handler, like so:

onsubmit="decideWhoToMailTo(this);"

The decideWhoToMailTo function would contain code like the following:

function decideWhoToMailTo(form) {
if (form.ru.value == "122") {
form.form.param_recipient = "(e-mail address removed)";
} else if (form.ru.value == "144") {
form.param_recipient = "(e-mail address removed)";
}
}

....Then, I'd probably wonder why I bothered coding any Javascript at
all, when all the processing could easily be done on the server end.
 
T

TeknoShock

It wasn't working, so I made some small changes to test. Here is the
relevant code:
---------------------------
<script language="javascript" >

function decideWhoToMailTo(dform) {
if (document.dform.ru.value == "122") {
document.dform.param_pie = "(e-mail address removed)";
} else if (document.dform.ru.value == "144") {
document.dform.param_pie = "(e-mail address removed)";
}

}

</script>
-----------------------------
<FORM name="dform"
ACTION="http://intranet.mycompany.org/cgi-bin/purchase2.pl" METHOD=POST
onsubmit="decideWhoToMailTo(this);" >

<INPUT TYPE="hidden" name="param_pie">

RU: <input type="text" name="ru" value="122" tabindex="2">
----------------------------
I then passed the param_pie to the cgi script just to see what it is
being set to. It is being set to blank.

Can you see where I went wrong? I am leaving the ru set to the default
value of 122, so I would expect param_pie to be set to (e-mail address removed).
 
M

mick white

TeknoShock said:
It wasn't working, so I made some small changes to test. Here is the
relevant code:
---------------------------
<script language="javascript" >

function decideWhoToMailTo(dform) {
if (document.dform.ru.value == "122") {
document.dform.param_pie = "(e-mail address removed)";
} else if (document.dform.ru.value == "144") {
document.dform.param_pie = "(e-mail address removed)";
}

}

function decideWhoToMailTo(dform) {
var d=dform.ru;
dform.param_pie.value=
d.value == "122"?"(e-mail address removed)":
d.value == "144"?"(e-mail address removed)":"no email";
}



Mick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top