Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gravity Wiz // Gravity Forms // Prevent Duplicate Submissions from Double Clicks
<?php
/**
* Gravity Wiz // Gravity Forms // Prevent Duplicate Submissions from Double Clicks
* http://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">
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;
}
@matgray87

This comment has been minimized.

Copy link

@matgray87 matgray87 commented Jan 17, 2018

Hi,
This is very useful for a form that loads a second page when the submit button is clicked, however, if it is an AJAX form, this snipped only works the first time, but not any subsequent submissions... what would you have to change to allow this to work every time the form is submitted using AJAX (assuming there were errors with the first form submissions).
Thanks,
Matt

@sperez91

This comment has been minimized.

Copy link

@sperez91 sperez91 commented Aug 27, 2020

Thank you so much for this! I loaded it on some of my websites using the Code Snippets Plugin.

@spivurno

This comment has been minimized.

Copy link
Owner Author

@spivurno spivurno commented Aug 28, 2020

Glad to help, @sperez91!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment