Skip to main content

Ajax Gravity Forms [top] -

$.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.

wp_send_json_error( array( 'validation_html' => $validation_html ) ); } else { // Success! Define a redirect URL (from confirmation or custom) $confirmation = GFFormDisplay::handle_confirmation( $form, $entry, false ); $redirect_url = is_array( $confirmation ) && isset( $confirmation['redirect'] ) ? $confirmation['redirect'] : home_url( '/thank-you/' );

function my_gf_ajax_submit_handler() { // Verify nonce if ( ! wp_verify_nonce( $_POST['security'], 'gf_ajax_nonce' ) ) { wp_die('Security check failed'); } $form_id = intval( $_POST['form_id'] ); $form = GFAPI::get_form( $form_id ); ajax gravity forms

For WordPress site owners, this traditional, synchronous form submission has long been the default behavior of Gravity Forms, the premium plugin powering millions of websites. It works. It's reliable. But in an era of single-page applications and instant feedback, the full-page reload feels clunky, disorienting, and slow.

Enter (Asynchronous JavaScript and XML). When combined with Gravity Forms, AJAX transforms the user experience from a jarring interruption into a seamless, fluid interaction. This piece explores the power, implementation, and best practices of using AJAX with Gravity Forms, turning a standard contact form into a modern, high-performance interface. What is AJAX, and Why Does It Matter for Forms? AJAX is a set of web development techniques that allows parts of a web page to update without reloading the entire page. Think of the "Like" button on social media: you click it, the count increments, and the heart turns red—all without the page refreshing. The same principle applies to forms. $validation_html = ob_get_clean()

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:

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(); wp_send_json_success( array( 'redirect_url' =&gt

wp_send_json_success( array( 'redirect_url' => $redirect_url ) ); } } add_action( 'wp_ajax_my_gf_submit_form', 'my_gf_ajax_submit_handler' ); add_action( 'wp_ajax_nopriv_my_gf_submit_form', 'my_gf_ajax_submit_handler' );