Ajax Gravity Forms <DIRECT — GUIDE>
However, this built-in solution, while powerful, is the "lowest common denominator." It works reliably, but it lacks customization. The confirmation message fades in, the errors appear, but you have limited control over what happens next . What if you want to redirect to a custom "thank you" page using AJAX ? What if you want to close a modal window upon successful submission? What if you need to track the submission in Google Analytics?
function my_gf_ajax_scripts() { if ( has_shortcode( get_post()->post_content, 'gravityform' ) ) { wp_enqueue_script( 'my-gf-ajax', get_template_directory_uri() . '/js/gf-ajax.js', array('jquery'), '1.0', true ); wp_localize_script( 'my-gf-ajax', 'my_ajax_obj', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'gf_ajax_nonce' ), ) ); } } add_action( 'wp_enqueue_scripts', 'my_gf_ajax_scripts' ); This script will find your form (using its ID, e.g., gform_1 ), override the submit behavior, and send the data via AJAX. ajax gravity forms
Gravity Forms uses JavaScript to handle conditional logic (showing/hiding fields). If you load a form via AJAX into a modal, you must re-initialize Gravity Forms' scripts: However, this built-in solution, while powerful, is the
if ( empty( $result['is_valid'] ) ) { // Validation failed. Get the validation HTML. ob_start(); GFFormDisplay::get_form( $form_id, true, true ); $validation_html = ob_get_clean(); What if you want to close a modal
$.ajax({ url: my_ajax_obj.ajax_url, type: 'POST', data: formData, beforeSend: function() { $form.find('input[type="submit"]').prop('disabled', true).val('Submitting...'); }, success: function(response) { if (response.success) { // Custom success behavior: Redirect! window.location.href = response.data.redirect_url; } else { // Display validation errors (Gravity Forms sends back HTML) $form.find('.gform_validation_errors').remove(); // Clear old errors $form.prepend(response.data.validation_html); $form.find('input[type="submit"]').prop('disabled', false).val('Submit'); } }, error: function() { alert('An error occurred. Please try again.'); $form.find('input[type="submit"]').prop('disabled', false).val('Submit'); } }); }); }); Finally, you need a PHP function that receives the AJAX request, tells Gravity Forms to process the submission, and returns a structured JSON response.
In the early days of the web, submitting a form was a dramatic event. You filled out your name, email, and message, clicked "Submit," and then... the screen went white. The browser churned, the page reloaded, and you found yourself either staring at a "Thank You" message at the top of a freshly rendered page or, more frustratingly, back at the same form, squinting to see which red error message had appeared.
This custom approach gives you complete control. You can close modal popups, play success sounds, trigger analytics events, or animate a custom thank-you message—all without ever leaving the page. Even with a solid understanding, AJAX and Gravity Forms can present challenges.