Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Location
    Southern California
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    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.
    Last edited by Philip M; 05-04-2012 at 07:55 PM.

    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.

  • Users who have thanked Philip M for this post:

    rmeyer7 (05-04-2012)

  • #3
    New to the CF scene
    Join Date
    May 2012
    Location
    Southern California
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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";
    
    }

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Posts
    165
    Thanks
    1
    Thanked 39 Times in 39 Posts
    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";
    }

  • Users who have thanked EpicWebDesign for this post:

    rmeyer7 (05-04-2012)

  • #5
    New to the CF scene
    Join Date
    May 2012
    Location
    Southern California
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Posts
    165
    Thanks
    1
    Thanked 39 Times in 39 Posts
    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.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    rmeyer7 (05-07-2012)

  • #8
    New to the CF scene
    Join Date
    May 2012
    Location
    Southern California
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •