I am using "contact form 7"
WordPress plugin.
I want to disable submit button on form submit and change text like
"Submitting...."
and enable on success or on error so the user can click again.
I am using "contact form 7"
WordPress plugin.
I want to disable submit button on form submit and change text like
"Submitting...."
and enable on success or on error so the user can click again.
The above answers didn't work for me, could be a conflict with the latest version of CF7.
Anyway, I've updated the code above so that it works with the latest version.
I've also improved the code so that it works with any form on a website, regardless of what the submit button says.
It disables the submit button, changes the value to ask the user to be patient, then when the form is finished submitting, the original submit value is restored.
/**
* Disable WPCF7 button while it's submitting
* Stops duplicate enquiries coming through
*/
document.addEventListener( 'wpcf7submit', function( event ) {
// find only disbaled submit buttons
var button = $('.wpcf7-submit[disabled]');
// grab the old value
var old_value = button.attr('data-value');
// enable the button
button.prop('disabled', false);
// put the old value back in
button.val(old_value);
}, false );
$('form.wpcf7-form').on('submit',function() {
var form = $(this);
var button = form.find('input[type=submit]');
var current_val = button.val();
// store the current value so we can reset it later
button.attr('data-value', current_val);
// disable the button
button.prop("disabled", true);
// tell the user what's happening
button.val("Sending, please wait...");
});
Please use this code to disable the submit button.
jQuery('.wpcf7-submit').on('click',function(){
jQuery(this).prop("disabled",true); // disable button after clicking on button
});
As we know that the contact form 7
plugin returns various responses after submitting.
This is for mail sent event:
document.addEventListener( 'wpcf7mailsent', function( event ) {
jQuery(this).prop("disabled",false);// enable button after getting respone
}, false );
see all events of contact form 7
Updated:
document.addEventListener( 'wpcf7submit', function( event ) {
var status = event.detail.status;
console.log(status);
//if( status === 'validation_failed'){
jQuery('.wpcf7-submit').val("Send");
//}
}, false );
jQuery('.wpcf7-submit').on('click',function(){
jQuery(this).val("Submitting....");
});
Note: status
returns the responses like validation_failed
, mail_sent
, etc after form submitted.
You can use this instead. This code will help you to disable submit button till sent the success email. You can stop multiple submit by this code.
// disable button after clicking on submit button
<?php add_action('wp_footer', 'mycustom_wp_footer');
function mycustom_wp_footer()
{
?>
<script type="text/javascript">
jQuery('.wpcf7-form').submit(function() {
jQuery(this).find(':input[type=submit]').prop('disabled', true);
var wpcf7Elm = document.querySelector('.wpcf7');
wpcf7Elm.addEventListener('wpcf7submit', function(event) {
jQuery('.wpcf7-submit').prop("disabled", false);
}, false);
wpcf7Elm.addEventListener('wpcf7invalid', function() {
jQuery('.wpcf7-submit').prop("disabled", false);
}, false);
});
</script>
<?php
}?>