<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>disable button | LogikDevelopment</title>
	<atom:link href="https://www.logikdev.com/tag/disable-button/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.logikdev.com</link>
	<description>&#34;Il n&#039;y a pas de problème, il n&#039;y a que des solutions. L&#039;esprit de l&#039;homme invente ensuite le problème.&#34; André Gide</description>
	<lastBuildDate>Tue, 25 Apr 2017 13:57:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.4</generator>
	<item>
		<title>Prevent double-click on HTML form</title>
		<link>https://www.logikdev.com/2011/06/29/prevent-double-click-on-html-form/</link>
					<comments>https://www.logikdev.com/2011/06/29/prevent-double-click-on-html-form/#comments</comments>
		
		<dc:creator><![CDATA[smoreau]]></dc:creator>
		<pubDate>Wed, 29 Jun 2011 21:04:10 +0000</pubDate>
				<category><![CDATA[Html]]></category>
		<category><![CDATA[disable button]]></category>
		<category><![CDATA[double-click]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[onclick]]></category>
		<category><![CDATA[onsubmit]]></category>
		<guid isPermaLink="false">https://www.logikdev.com/?p=1336</guid>

					<description><![CDATA[<p>How many users are still double-clicking when they visit a website? It is not really a problem with links, but it could be with buttons. Let&#8217;s take, for example, a &#8216;contact us&#8217; form and a submit button which will send the content of the form by email to the website administrator. What happens if a [&#8230;]</p>
<p>The post <a href="https://www.logikdev.com/2011/06/29/prevent-double-click-on-html-form/">Prevent double-click on HTML form</a> first appeared on <a href="https://www.logikdev.com">LogikDevelopment</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>How many users are still double-clicking when they visit a website? It is not really a problem with links, but it could be with buttons.<br />
Let&#8217;s take, for example, a &#8216;contact us&#8217; form and a submit button which will send the content of the form by email to the website administrator. What happens if a user double-click on the submit button? The form will be submitted twice and in consequence two emails will be sent instead of one.</p>
<p>There are multiple ways to fix this problem. The solution I will explain below is to disable the submit button once it has been pressed. In that way, even if the user double-clicks on it, the form will be submitted only once! 🙂</p>
<p>To disable the submit button once it has been pressed, you simply need to add the following JavaScript code in the <code>onclick</code> attribute of your button tag:</p>
<pre class="brush: jscript; light: true; title: ; notranslate">
this.disabled=true;this.form.submit();
</pre>
<p>For example:</p>
<pre class="brush: xml; light: true; title: ; notranslate">
&lt;input name=&quot;submitButton&quot; id=&quot;submitButton&quot; type=&quot;submit&quot; value=&quot;Submit&quot; onclick=&quot;this.disabled=true;this.form.submit();&quot; /&gt;
</pre>
<p>The code above will work fine if you don&#8217;t have an <code>onsubmit</code> attribute in your form tag. But, if you have one, you will notice that the function <code>this.form.submit()</code> will not execute the content of the <code>onsubmit</code> attribute. In that case, you need to force the call as shown below:</p>
<pre class="brush: jscript; light: true; title: ; notranslate">
this.disabled=true;if(this.form.onsubmit()){this.form.submit();}else{this.disabled=false;}
</pre>
<p>However, it is recommended to move whatever you have in the <code>onsubmit</code> attribute in the button onclick, such as:</p>
<pre class="brush: jscript; light: true; title: ; notranslate">
this.disabled=true;if(validateForm()){this.form.submit();}else{this.disabled=false;}
</pre>
<p><strong>NOTE:</strong><br />
If in your code, you are using the following to check if the form has been submitted:</p>
<pre class="brush: php; light: true; title: ; notranslate">
if (isset($_POST&#x5B;'submitButton'])) { echo &quot;Form submitted!&quot;; }
</pre>
<p>It will need to be replaced by the following:</p>
<pre class="brush: php; light: true; title: ; notranslate">
if ($_SERVER&#x5B;'REQUEST_METHOD'] == &quot;POST&quot;) { echo &quot;Form submitted!&quot;; }
</pre><p>The post <a href="https://www.logikdev.com/2011/06/29/prevent-double-click-on-html-form/">Prevent double-click on HTML form</a> first appeared on <a href="https://www.logikdev.com">LogikDevelopment</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.logikdev.com/2011/06/29/prevent-double-click-on-html-form/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
