Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-04-2012, 07:46 PM   PM User | #1
rmeyer7
New to the CF scene

 
Join Date: May 2012
Location: Southern California
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
rmeyer7 is an unknown quantity at this point
Novice question about emailing form submissions

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.

Can anyone help?? Thanks!
rmeyer7 is offline   Reply With Quote
Old 05-04-2012, 07:52 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.

Last edited by Philip M; 05-04-2012 at 07:55 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
rmeyer7 (05-04-2012)
Old 05-04-2012, 08:35 PM   PM User | #3
rmeyer7
New to the CF scene

 
Join Date: May 2012
Location: Southern California
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
rmeyer7 is an unknown quantity at this point
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?

Code:
if (document.bobForm.req_Division[document.bobForm.req_Division.selectedIndex].value == "California"){

	if(bobForm.req_District.value = "43")

document.bobForm.sendTo.value = "recipient@company.com";

}
rmeyer7 is offline   Reply With Quote
Old 05-04-2012, 09:07 PM   PM User | #4
EpicWebDesign
Regular Coder

 
Join Date: Apr 2012
Posts: 165
Thanks: 1
Thanked 39 Times in 39 Posts
EpicWebDesign will become famous soon enough
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:
Code:
if (document.bobForm.req_Division[document.bobForm.req_Division.selectedIndex].value == "California" && bobForm.req_District.value == "43"){

document.bobForm.sendTo.value = "recipient@company.com";
}
EpicWebDesign is offline   Reply With Quote
Users who have thanked EpicWebDesign for this post:
rmeyer7 (05-04-2012)
Old 05-04-2012, 09:28 PM   PM User | #5
rmeyer7
New to the CF scene

 
Join Date: May 2012
Location: Southern California
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
rmeyer7 is an unknown quantity at this point
Awesome, thank you both for the input. I think this will work perfectly!

Also, just in response to the question, there won't be very many recipients...only a few of the divisional executives actually want the info emailed.
rmeyer7 is offline   Reply With Quote
Old 05-04-2012, 09:37 PM   PM User | #6
EpicWebDesign
Regular Coder

 
Join Date: Apr 2012
Posts: 165
Thanks: 1
Thanked 39 Times in 39 Posts
EpicWebDesign will become famous soon enough
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..
EpicWebDesign is offline   Reply With Quote
Old 05-05-2012, 12:52 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
rmeyer7 (05-07-2012)
Old 05-07-2012, 04:05 PM   PM User | #8
rmeyer7
New to the CF scene

 
Join Date: May 2012
Location: Southern California
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
rmeyer7 is an unknown quantity at this point
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...
rmeyer7 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:20 PM.


Advertisement
Log in to turn off these ads.