The Problem
Some forms take a few seconds to submit, especially with file uploads, payments or add-on feeds. During that time the only sign that anything is happening is a small spinner, so visitors wonder whether it worked. You’d like the submit button itself to change to something like “Sending, please wait…”.
The snippet I shared here in 2021 no longer works: Gravity Forms now uses a <button> for the submit button instead of an <input>, and the old code causes a PHP error on current versions. Use the version below instead.
The Solution
Gravity Forms has a JavaScript hook that runs just before a form is submitted, gform/submission/pre_submission. This small plugin uses it to change the button text, and puts the original text back if the submission is stopped (for example by another script).
1. Add the code
Save this as a plugin file (for example gr-submit-button-text.php in wp-content/plugins, then activate it), or paste everything after the opening <?php into a PHP snippet in WPCode.
<?php
/**
* Plugin Name: Gravity Ranger: Change Submit Button Text While Sending
* Description: Changes the Gravity Forms submit button text while the form is submitting.
* Version: 1.0.0
*/
add_action( 'wp_footer', function () {
if ( ! class_exists( 'GFForms' ) ) {
return;
}
?>
<script>
document.addEventListener( 'gform/theme/scripts_loaded', function () {
var sendingText = 'Sending, please wait…'; // Change this text.
var onlyForms = []; // Limit to form IDs, e.g. [3, 7]. Empty = all forms.
gform.utils.addFilter( 'gform/submission/pre_submission', function ( data ) {
var formId = parseInt( data.form.dataset.formid, 10 );
if ( data.abort || data.submissionType !== 'submit' || ( onlyForms.length && onlyForms.indexOf( formId ) === -1 ) ) {
return data;
}
var button = data.form.querySelector( '#gform_submit_button_' + formId );
if ( button ) {
var isInput = button.tagName === 'INPUT';
button.dataset.grOriginalText = isInput ? button.value : button.textContent;
if ( isInput ) {
button.value = sendingText;
} else {
button.textContent = sendingText;
}
}
return data;
}, 20 );
document.addEventListener( 'gform/submission/submission_aborted', function ( event ) {
var form = event.detail && event.detail.form;
var button = form && form.querySelector( '[data-gr-original-text]' );
if ( button ) {
if ( button.tagName === 'INPUT' ) {
button.value = button.dataset.grOriginalText;
} else {
button.textContent = button.dataset.grOriginalText;
}
delete button.dataset.grOriginalText;
}
} );
} );
</script>
<?php
}, 100 );
2. Change the text or limit it to some forms
Edit sendingText for your own wording. To use it only on certain forms, put their IDs in onlyForms, for example [3, 7].
3. Test it
Submit a test entry. The button text changes as soon as you click and the form goes through as usual. I tested it on Gravity Forms 3.2 with the Advanced Honeypot and Submission Speed Check turned on: the entry was saved normally, not as spam, and when a submission was cancelled the button went back to its original text.
Things to Know
- It only changes the text on the final submit, not on Next or Previous buttons in multi-page forms, or on Save and Continue.
- You don’t need to disable the button. Gravity Forms already ignores extra clicks while a form is submitting.
- If a form fails validation, Gravity Forms shows it again with the original button text, so there’s nothing to reset.
- The code uses Gravity Forms’ current JavaScript hooks, so keep Gravity Forms up to date. I tested it on version 3.2.
- If your forms are slow to submit, the cause is usually a slow add-on feed, email sending or the server, not the form itself. The button text helps visitors wait, but it’s worth finding the slow step.
Need More Help?
If you still need help, use the chat or leave a comment below and we’ll get back to you.
Comments