<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">

<channel>

 <title>Professors Coding Corner</title>
 <link>http://www.professorscodingcorner.com/</link>
 <description>The coding corner has tutorials and code snippets in both Javascript and Perl.</description>
 <lastBuildDate>Sun, 25 Dec 2011 17:37:20 GMT</lastBuildDate>
 <language>en-us</language>
 
 <image>
  <title>Professors Coding Corner</title>
  <url>http://www.professorscodingcorner.com/gifs/logo.gif</url>
  <link>http://www.professorscodingcorner.com/</link>
  <width>106</width>
  <height>130</height>
 </image>
 
 <item>
  <title>Form Submission - No Double-click Possible | Professor's Coding Corner</title>
  <link>http://www.professorscodingcorner.com/snippets/formsubmit.shtml</link>
  <guid>http://www.professorscodingcorner.com/snippets/formsubmit.shtml</guid>
  <category>no double click, double click problem, form submission script, form submit button, html form code</category>
  <pubDate>Sun, 25 Dec 2011 17:37:20 GMT</pubDate>
  <description><![CDATA[<h2>Code Snippet: Form Submission - No Double-Click Possible</h2>
<p>One of my clients once complained that she frequently received two copies of emails sent to her from her website contact form.&nbsp; I told her I would find a solution to the double-click problem, and began a Google search to see if any suitable scripts were already available.&nbsp; I figured that something must be out there, since it would be pretty serious if someone clicked twice on the form "Submit" button for a credit-card purchase.</p>
<p class="newsection"><b>Description of the script:</b></p>
<p>1) In order to further my goal of using <span title="Unobtrusive: seperating dynamic behavior (the script) from structure (the HTML)" class="hoverTip">unobtrusive</span> scripting, the script is located in an external file.</p>
<p>2) Keeping the script off the webpage has a beneficial side-effect.&nbsp; Some spammers use robots which can harvest the address of your form-handling script and use it to send unwanted emails to your own website mailbox.&nbsp; This script dynamically adds this address when the form is submitted.&nbsp; It is never visible in the HTML form code of your webpage.</p>
<p>3) After one click on the form "Submit" button, the text on the button is changed to "Please Wait", and it turns red (or any other color you wish).</p>
<p>4) The script keeps track of that first click, and if the "Submit" button is clicked again, bypasses a second form submission.</p><pre>
 1  var count = 0;
 2  function submitForm()
 3  {
 4   if (!document.getElementById) return false;
 5   var submit = document.getElementById("submit");
 6   var form = document.getElementById("form");
 7   submit.onclick = function()
 8   {
 9     // Insert checks for proper form fields here
10     if (count == 0)
11     {
12       count++;
13       var url = "[URL of CGI form-submission script]";
14       form.setAttribute("action",url);
15       submit.setAttribute("value","Please Wait");
16       submit.style.backgroundColor = "red";
17       form.submit;
18       return true;
19     }
20     return false;
21   }
22   return true;
23 } 
</pre>
<p class="newsection"><b>How to use the script:</b></p>
<p>1) Substitute the address of your CGI form-handling page for the [ ] in line 13.</p>
<p>2) In place of line 9, you may want to insert code to check for the proper input of form fields, such as was presented in a previous snippet, <a href="emailcheck.shtml" title="go to the 'Alert Box - Email Form Field Required' snippet page">Alert Box - Email Form Field Required</a>.</p>
<p>3) Follow the instructions on the <a href="../snippets.shtml" title="go to the Snippets Page">main Snippets Page</a> for how to install a javascript function that runs at load time.</p>
<p>4) Make sure that your &lt;form&gt;, and &lt;submit&gt; tags contain the id&rsquo;s specified in lines 5-6 of the script.&nbsp; This script and your CSS stylesheet can use the same id&rsquo;s.</p>
<p>5) The HTML form code should NOT contain the URL of your form-handling page, if you want to spam-proof your form. The &lt;form&gt; tag should look something like:</p>
<pre>
&lt;form id="form" method="post" action="#"&gt;
</pre>
<p>6) Finally, there is one last thing that is not required, but I always use whenever a script does not <span title="degrade: work even if the user has javascript disabled" class="hoverTip">degrade</span> well. Just before the HTML form code starts, insert an explanation for users that don&rsquo;t have javascript, such as:</p>
<pre>
&lt;noscript&gt;
  &lt;h3 style="color:red"&gt;JavaScript is required to use this form.&lt;/h3&gt;
  &lt;p&gt;For instructions on enabling JavaScript,&lt;br /&gt;
  please see &lt;a href="http://www.professorsopportunities.com/javascript.html"&gt;
  this page&lt;/a&gt;.&lt;/p&gt;
&lt;/noscript&gt;
</pre>
<p>Most users will never see the contents of this tag.&nbsp; I hate using non-degradable javascript, but in this case it is necessary to achieve the benefit of spam-proofing the form.&nbsp; (However, if you don&rsquo;t like this, you can put the real URL back in the &lt;form&gt; tag to make the form available even for retrograde users.)</p>
<p style="margin-bottom:-10px;"><strong>Update - December 19, 2011:</strong> I found a much shorter script than the one above at <a href="http://www.revolutionwebdesign.com/blog/index.cfm/2004/10/20/prevent-doubleclick-form-submit" rel="nofollow" title="">RevolutionWebDesign.com</a> <img src="gifs/link.gif" class="external" alt="external link" title="external link" />.&nbsp; All you need to do is insert the following attribute in your form submit tag:</p>
<pre>
onclick="this.disabled=true;this.form.submit();"
</pre>
<p style="margin-top:-10px;">Of course, this script is not off-page, and it lacks two of the salient features of my script: 1) it doesn't hide the URL of the CGI form-handling page from spammers, and 2) it lacks visual confirmation that the form has been submitted.</p>
]]></description>
 </item>

</channel>
</rss>
