Overview
Gravity Forms 2.9 introduced a new way to submit forms: the AJAX submission method. It sends the form submission to the server using an AJAX request, without reloading the page. This also gives add-ons new tools to work more directly with third-party services such as payment gateways.
The Stripe Add-On is the first add-on built on it, and other payment add-ons will follow.
Submission Methods
Gravity Forms supports three submission methods:
| Method | Value | Description |
|---|---|---|
| Postback | postback | The default. The form is submitted and the page reloads. |
| Legacy AJAX | iframe | The previous “AJAX” option. The form is submitted through a hidden iframe, so the page doesn’t reload, but it isn’t a true AJAX request. |
| AJAX | ajax | Added in Gravity Forms 2.9. The form is submitted using a true AJAX request. |
Note: If you’ve enabled AJAX on a form in the past, for example, with the ajax="true" shortcode attribute or the block setting, that form uses legacy AJAX, not the new AJAX submission method.
Why We Added It
The older submission methods weren’t designed for payment gateways that confirm payments in the browser. To make them work, add-ons had to adapt the payment gateway’s process to fit the Gravity Forms submission process.
This could leave things in an inconsistent state. For example, if a customer closed the browser or navigated away partway through, the Stripe payment could go through without a matching entry being created in Gravity Forms.
How It Works With Stripe
Starting with Stripe Add-On 7.0, the order of events on submission has changed:
- The form is submitted via AJAX.
- The entry is created first, before any payment is created in Stripe.
- The payment is then created and processed via a separate AJAX request linked to the existing entry.
Because the entry always exists before the payment, a customer cannot be charged without an entry being created.
Enabling the AJAX Submission Method
There’s no setting in the form editor for the AJAX submission method. To enable it for your forms, use the gform_form_args filter and set submission_method to GFFormDisplay::SUBMISSION_METHOD_AJAX.
This example enables it for all forms:
add_filter( 'gform_form_args', function ( $args ) {
$args['submission_method'] = GFFormDisplay::SUBMISSION_METHOD_AJAX;
return $args;
} );
To enable it only for specific forms, see the Enable new AJAX.
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Supported Add-Ons
| Add-On | Version |
|---|---|
| Stripe Add-On | 7.0 and later |
Other payment add-ons will be added to this list as they are updated to use the AJAX submission method.