Software, your way.
burger menu icon
WillMaster

WillMasterBlog > JavaScript

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Different Email Destinations For Different Form User Locations

I will show you how to send email to a different destination if the form user does not specify a certain geographic location.

A form for Master Form V4 is used for the example. The idea can be implemented in other forms.

Master Form V4 uses a hidden field name="emailtemplate" to specify which template file to use when constructing the email to be sent to the site owner (or to the form user or subscription service or wherever).

When the form is submitted, JavaScript checks to see whether or not "Iowa" was selected from the dropdown list. If not, the value of name="emailtemplate" is changed.

The source code for the complete example is below. After the source code, I'll talk a bit about how to change the code without breaking it.

<script type="text/javascript"><!--
function DetermineEmailTemplate(sp) {
if( sp.State_Province.value != "IA" )
   { sp.emailtemplate.value = "templates/Elsewhere.txt"; }
return true;
}
//--></script>


<form 
   method="post" 
   onsubmit="return DetermineEmailTemplate(this)"
   action="/cgi-bin/MasterFormV4.cgi">

<input 
   type="hidden" 
   name="emailtemplate" 
   value="templates/Iowa.txt">

<input type="hidden" name="redirect" value="/thankyou.html">
<input type="hidden" name="required" value="name, email, State_Province">

<p>
Name: <input type="text" name="name" size="22"><br />
Email: <input type="text" name="email" size="22">
</p>

<select name="State_Province">
<option value=""> </option>
<option value="">--- United States ---</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
<option value=""> </option>
<option value="">--- US Territories ---</option>
<option value="AS">American Samoa</option>
<option value="FS">Fed St Micronesia</option>
<option value="GU">Guam</option>
<option value="MH">Marshall Islands</option>
<option value="MP">Northern Mariana Is</option>
<option value="PW">Palau Island</option>
<option value="PR">Puerto Rico</option>
<option value="VI">U.S. Virgin Islands</option>
<option value=""> </option>
<option value="">--- Canada ---</option>
<option value="AB">Alberta</option>
<option value="BC">British Columbia</option>
<option value="MB">Manitoba</option>
<option value="NB">New Brunswick</option>
<option value="NF">Newfoundland</option>
<option value="NT">Northwest Territory.</option>
<option value="NS">Nova Scotia</option>
<option value="NT">Nunavut</option>
<option value="ON">Ontario</option>
<option value="PE">Prince Edward Island</option>
<option value="PQ">Quebec</option>
<option value="SK">Saskatchewan</option>
<option value="YK">Yukon</option>
<option value=""> </option>
<option value="">--- Other ---</option>
<option value="OT">Other</option>
</select>

<p>
<input type="submit" name="submitter" value="Send Information">
</p>

</form>

The above is a working example.

If you do not have Master Form V4 or you wish to match or change other values:

  • If appropriate, change the <form... tag's action attribute to use the form handling software you prefer.

  • The onsubmit="return DetermineEmailTemplate(this)" attribute needs to be in the <form... tag.

  • The name="emailtemplate" field is initialized for the email template file to be used when the form user is located in the state of Iowa, USA. That can, of course, be changed. Whatever value the name="emailtemplate" field is initialized with will remain if the browser has JavaScript disabled.

    If your software requires a field name different than "emailtemplate", go ahead and change the field name.

  • The JavaScript needs to be somewhere above or below the form, not within the form

  • If your software requires a field name different than "emailtemplate", go ahead and change the field name in the JavaScript to be identical to the field name in the form.

  • If the field value to skip is something other than "IA", change the JavaScript accordingly. (In the example, the dropdown list item "Iowa" has a value of "IA".)

Those are some of the changes that can be made without breaking the form or the JavaScript and the way they work together.

Will Bontrager

Was this blog post helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method


All information in WillMaster Blog articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

Recent Articles in the Library

Submit Form Data Without Bothering User

To silently and effectively submit a form on a web page, Ajax with FormData() can be used.

CSS Dots

CSS dots can be used for interactions between web page and website user.

Email Testing

One thing to test when email doesn't get sent or doesn't arrive is whether the server actuall sends email.

Tap to Select

Find out how to make a link or button to select the content in a div or other HTML element.

Capitalizing the First Letter of Words

Here, find both PHP code and JavaScript code to capitalize the first letter of a word.

Fixed-position Table Header

During a vertical scroll, the table header scrolls out of view -- unless the header is fixed in position.

Simple Floating Menu

When not needed, the menu on the left side of a web page retracts to be out of the way. Tap it and it expands.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC