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