Prevent Duplicate Submissions from Double Clicks

Disable the submit button after the initial click to prevent double clicks.

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.

Code

Filename: gw-prevent-duplicate-submissions.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
 * Gravity Wiz // Gravity Forms // Prevent Duplicate Submissions from Double Clicks
 * https://gravitywiz.com/
 *
 * Disable the submit button after the initial click to prevent double clicks.
 *
 * Plugin Name:  Gravity Forms - Prevent Duplicate Submissions from Double Clicks
 * Plugin URI:   https://gravitywiz.com/
 * Description:  Disable the submit button after the initial click to prevent double clicks.
 * Author:       Gravity Wiz
 * Version:      1.0
 * Author URI:   https://gravitywiz.com/
 */
add_filter( 'gform_pre_render', 'gw_disable_submit' );
function gw_disable_submit( $form ) {

	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
		return $form;
	}

	?>

	<script type="text/javascript">

		document.addEventListener('DOMContentLoaded', function() {
			if (typeof jQuery !== 'undefined') {
				jQuery( document ).ready( function( $ ) {

				var formId = '<?php echo $form['id']; ?>';

				$( '#gform_submit_button_' + formId ).on( 'click', function( event ) {

					if( hasPendingFileUploads( formId ) ) {
						return;
					}

					var $submitCopy = $( this ).clone();

					$submitCopy
						.attr( 'id', '' )
						.prop( 'disabled', true )
						.attr( 'value', 'Processing...' )
						.insertBefore( $( this ) );

					$( this ).css( { visibility: 'hidden', position: 'absolute', transition: 'all 0s ease 0s' } );

				} );

				function hasPendingFileUploads() {

					if( ! window[ 'gfMultiFileUploader' ] ) {
						return false;
					}

					var pendingUploads = false;

					$.each( gfMultiFileUploader.uploaders, function( i, uploader ) {
						if( uploader.total.queued > 0 ) {
							pendingUploads = true;
							return false;
						}
					} );

					return pendingUploads;
				}

				} );
			}
		});

	</script>

	<?php

	return $form;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.