Auto select the contact form recipient from a drop down.

If you have a program or script that you'd like to give away or sell, this is the place to do it. All offers should include contact info.

Moderators: BBear, theunknownhost, flaguy

Post Reply
User avatar
Sapphyre
Official Test Penquin
Posts: 3337
Joined: Tue Nov 19, 2002 2:50 am
Location: Canada

Auto select the contact form recipient from a drop down.

Post by Sapphyre » Fri Mar 10, 2006 8:10 pm

I hope someone else can make use of this; works with IE and NS, don't know about other browsers.


Situation: School council wants to funnel all contact through a single form. Instead of providing differing mailto: links on each page (mailto:fundraising.committee@school.tld), all pages will link back to a single form where visitor is expected to select the intended recipient. i.e. Sometimes the visitor will not know who to select on the form whereas a mailto: link would be obvious.

So, instead of linking to just
http://school.tld/contactcouncil.htm
The links will read like this:
http://school.tld/contactcouncil.htm?for=fundraising

This page contactcouncil.htm contains an ordinary form designed for formmail, it's important to name the form and be sure it matches in the script
<form name="form1" method="post" action...

and using aliases for the recipients:

Code: Select all

Intended Recipient of your message: 
<select name="recipient">
                        <option value="general">I don't know</option>
                        <option value="volunteering">Volunteer Co-ordinator</option>
                        <option value="executives">Council Executives</option>
                        <option value="fundraising">Fundraising Committee</option>
</select>


Add this to the head:

Code: Select all

<script language="JavaScript">
<!-- Hide from incompatible browsers
function getRecip() {
   var query= window.location.search.substring(1);
   var pattern = /for=(\w+)/;
   if (pattern.test(query)) {
      var vars= query.split("&");
      for (var i=0;i<vars.length;i++) {
         var pair=vars[i].split("=");
         if (pair[0]=="for") { var recip = pair[1]; break; }
      }
   } else {  var recip = 'general'; }
   for ( var j=0;j<document.form1.recipient.options.length;j++) {
      if (document.form1.recipient.options[j].value==recip) {
         document.form1.recipient.selectedIndex = j;
         break;
      }
   }
}
//-->
</script>


and this to the body tag
<Body onload="getRecip();" ...

What this will do is automatically select the appropriate recipient in the dropdown depending on what value for "for=" is in the query string of the URL, defaulting to 'general' if none is specified or no match is found from the recipient list.

User avatar
Arf
Official Test Penquin
Posts: 9103
Joined: Tue Apr 09, 2002 12:00 am
Location: IDAHO, USA
Contact:

Post by Arf » Fri Mar 10, 2006 10:50 pm

I think this is awesome. I don't fully understand the Javascript but then I dn't really have to. Nice piece of work!

User avatar
Sapphyre
Official Test Penquin
Posts: 3337
Joined: Tue Nov 19, 2002 2:50 am
Location: Canada

Post by Sapphyre » Sat Mar 11, 2006 3:24 am

Thanks, Arf.

At least the concept is useful and the desired result is achieved. I'm not sure about the execution though; I don't do much javascript ... like PHP, I have to look everything up ;)

I only perl by heart!

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest