重力形式 - 获取当前页码

时间:2013-02-28 19:46:40

标签: php javascript wordpress wordpress-plugin

我有一个多页表单。

我想在此表单的最后一页上执行一些自定义JavaScript。从理论上讲,我所要做的就是检索当前的页码并写一个条件。

简单,对吧?显然不是。

我原来的解决方法是这样的:

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

但是$('...').css('display')会在在表单中尝试过的每个元素上返回undefined。每次用户点击“下一步”按钮时,都会触发自定义脚本。没有雪茄。

然后,在审核the Gravity Forms documentation后,我发现了两个看似有用的事件:gform_post_rendergform_page_loaded

但是,文档没有说明如何访问参数。

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(as the documentation suggests)中尝试了以下内容。 :

<?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);
?>

问题:

我需要哪些代码才能检索当前页码,更重要的是,我在哪里放置该代码?

7 个答案:

答案 0 :(得分:6)

我明白了。

功能rgpost显然对访问当前页码至关重要。经过我自己的混乱之后,我能够在functions.php和header.php中的wp_head()函数之前获得以下代码。

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替换为您的表单ID

我觉得有些资源很有用:

答案 1 :(得分:5)

OP接受的答案可能会有效,但如果您使用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 :(得分:0)

我写了一个返回当前页面的小函数:

// 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;
}

答案 3 :(得分:0)

除了接受的答案之外,这里是一个查找当前页面并动态查找表单中有多少页面的示例。此示例根据是否是表单的最后一页更改按钮文本,而不是将脚本排入队列。

// Change the text of the 'Next' form button to 'Submit' when
// the current page is the final page of the form
add_filter( 'gform_next_button', 'next_to_submit', 10, 2 );
function next_to_submit( $next_button, $form ) {
    $replacement_next_button = $next_button;
    $last_page_number = 1;
    foreach ($form['fields'] as $field) {
        $is_hidden = RGFormsModel::is_field_hidden( $form, $field, array() );
        if ($field['pageNumber'] > $last_page_number && !$is_hidden) {
            $last_page_number = $field['pageNumber'];
        }
    }
    $current_page_number = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;
    if ($last_page_number === $current_page_number) {
        // If the current page is the final page
        $replacement_next_button = str_replace( 'Next', 'Submit', $next_button );
    }
    return $replacement_next_button;
}

答案 4 :(得分:0)

当前接受的答案仅在用户从未访问过表单的上一页时有效,如果这样做,则gform_sourge_page_number总是滞后一个。我找到了一个更好的解决方案(以该钩子为例,但是您应该可以在传递了$ form的任何钩子中使用它):

function run_script_on_last_page( $form)  {
  if ( !is_admin() ) {
    if ( \GFFormDisplay::get_current_page( $form['id'] ) == 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' );

GFFormDisplay::get_current_page( $form_id )是许多方便的未记录功能之一。

答案 5 :(得分:0)

以下代码在JavaScript中对我有用:

$('.gf_step_active .gf_step_number').html()

它将以多页形式为您提供当前页面的页码。

答案 6 :(得分:0)

使用Jquery

  var current_page_ = 1+parseInt(jQuery('.gform_page:visible').index());
相关问题