Gravity Forms - Get Current Page Number

I have a multi-page form.

I would like to execute some custom JavaScript on the last page of this form. Theoretically, all I need to do is get the current page number and write a conditional expression.

Simple, right? Apparently not.

My initial decision was this:

if ($('gform_page').last().css('display') !== 'none') {
    // perform custom scripts now that the last
    // Gravity Form page is being shown
}

but $('...').css('display')returns undefinedfor every element I tried on the form. User scripts were run every time the user clicked the "Next" button. No cigars.

Then, looking at the documentation for Gravity Forms documents , I found two useful types of events: gform_post_renderand gform_page_loaded.

However, the documentation does not provide instructions on how to access the parameters.

jQuery(document).bind('gform_page_loaded', function(event, form_id, current_page){
    console.log(current_page);
    // returns nothing when loaded in the footer
    // returns [Object, object] when placed in an HTML field in the form
});

, , , functions.php header.php( ):

<?php
function enqueue_custom_script($form, $is_ajax){
    if ($is_ajax) :
        echo '<script>console.log(current_page);</script>';
    endif;
}
add_action("gform_enqueue_scripts", "enqueue_custom_script", 10, 2);
?>

:

, , ?

+5
3

.

rgpost, -, . , functions.php, wp_head() header.php.

function run_script_on_last_page($form) {
    if (!is_admin()) {
        $current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;
        if ($current_page == 10) {
            wp_enqueue_script('custom_script', get_template_directory_uri() . '/js/custom_script.js', array('jquery'), null, true);
        }
    }
}
add_action('gform_enqueue_scripts_63', 'run_script_on_last_page');

/ , , :

  • 10 , .
  • , wp_enqueue_script
  • 63

, :

+5

OPs , , Ajax.

Javascript ;

http://www.gravityhelp.com/documentation/page/Gform_post_render

jQuery(document).bind('gform_post_render', function (event, formId, current_page) {
    if (current_page == 2) {
        // do something
    }
});

, formId,

+2

, :

// Get Gravity Forms Current Page
// Syntax: gf_current_page()
function gf_get_current_page()
{
    return rgpost('gform_source_page_number_' . $_POST['gform_submit']) ? rgpost('gform_target_page_number_' . $_POST['gform_submit']) : 1;
}
0

All Articles