I know almost nothing about javascript, but the guy who used to handle my company's forms has moved on and I need to edit one.
Specifically, the form has a field for a district number, and I need to set it up so that if a certain district number is entered, the form submission gets emailed to a specified email address.
I'm not trying to make it an email-only form - the results have to be posted in an output file as well.
I've tried googling for an answer but haven't found anything that answers my question. Maybe that's because I don't know enough javascript to understand the answers.
You should use server-side scripting as mailto: is very unreliable.
Code:
var distnum = document.getElementById("districtnumber").value; // a form field
if (distnum == 12345) {
// send the details to a server-side email script
}
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
OK, I did some digging and it looks like there is an email handler script being used already. I think this is what I want to add to, but can you take a look at these code samples and let me know if I'm understanding this correctly?
How it's being used now:
Code:
if (document.bobForm.req_Division[document.bobForm.req_Division.selectedIndex].value == "Texas"){
if(bobForm.req_District.value != "c1" && bobForm.req_District.value != "c2" && bobForm.req_District.value != "c3" && bobForm.req_District.value != "d1" && bobForm.req_District.value != "d2" && bobForm.req_District.value != "d3" && bobForm.req_District.value != "d4" && bobForm.req_District.value != "C1" && bobForm.req_District.value != "C2" && bobForm.req_District.value != "C3" && bobForm.req_District.value != "C4" && bobForm.req_District.value != "D1" && bobForm.req_District.value != "D2" && bobForm.req_District.value != "D3" && bobForm.req_District.value != "D4"){
alert("You did not have a valid district number for this division. Please make sure you selected the correct division and enter your 2 digit district number.");
document.bobForm.req_District.value = "";
}
document.bobForm.sendTo.value = "recipient1@company.com, recipient2@company.com, recipient3@company.com ";
}
So my understanding is, it verifies the district is part of the correct division, and if everything checks out it sends the form to the email addresses listed, right?
If that's correct, would I be able to add something like this to make it do what I need?
You are on the right track... The initial code you provided checked to make sure the 2-digit District Code was valid for the Division. If is was, it sent the email. If it wasn't, it alerted them to the error.
With that in mind, you could certainly set it up so that if the Division and District Codes were all correct, it would send to a specific recipient. The problem you might run into though is the number of people you are dealing with (I have no idea how many different recipients you have.) Hard-coding each person into the equation could get very time consuming if there are too many.
Assuming there are only a few people, you could do what you suggested, but simplify the code as follows:
I just sent you a PM, but based on your updated response here, you may want to create a variable for each executive to save you time. For instance:
Code:
var Exec1 = recipient1@company.com;
var Exec2 = recipient2@company.com;
then, when you're doing the "sendTo" portion, you could just enter the variable for the correct person (Exec1, Exec2, etc.) instead of typing the whole email address each time. This would also save time if someone changes their email or a new person takes over. You'd only have to make the change once instead of throughout the whole script.
Last edited by EpicWebDesign; 05-04-2012 at 09:51 PM..
You could also change from the mass of "if" test to something simpler:
Code:
var addressees = {
"California" : {
"43" : Exec1, /* using EpicWebDesign's suggestion! */
"67" : "joe@abc.com; bob@xyz.com",
"other" : "headquarters@foo.com"
},
"Texas" : {
"17" : "somebody@here.com",
"22" : "spam@there.com; reportme@spam.com",
"other" : "demo@demo.com"
}
};
var form = document.bobForm;
var division = form.req_Division.value;
var district = form.req_District.value;
var state = addressees[division]; // find the right division in the addressees;
if ( state == null )
{
...error? state not valid? ...
}
var emails = state[district];
if ( emails == null )
{
emails = state["other"]; // no matching district...use the default for this division
}
form.sendTo.value = emails;
...
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Now that would be a big improvement. But there are about 5 other people who also rely on this script (each of us handle only selected divisions) and they know it and are fine with it the way it is written. I'm the new guy on the team, so I don't want to rock the boat by saying we should redo what they've already done...