I have a multi-page form.
I would like to execute some custom JavaScript on the last page of this form. Theoretically, all I have to do is retrieve the current page number and write a conditional.
Simple, right? Apparently not.
My original workaround was like 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 undefined
on every element I’ve tried this on within the form. Custom scripts were being fired every time the user hit the “Next” button. No cigar.
Then, after reviewing the Gravity Forms documentation, I found two useful-looking events: gform_post_render
and gform_page_loaded
.
However, the documentation gives no instruction 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
});
In addition to not having the correct code, I also suspect I don’t have the code in the correct place as I have also fruitlessly tried the following in functions.php and in 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);
?>
Question:
What code do I need to retrieve the current page number, and more importantly, where do I place that code?
I got it.
The function
rgpost
is, apparently, crucial in accessing the current page number. After some muddling around on my own, I was able to get the following code working in both functions.php and just before the wp_head() function in header.php.If you’re copy/pasting the code above, make sure to:
10
with the page you want to checkwp_enqueue_script
63
with your form IDSome resources I found useful:
gform_validation
filtergform_enqueue_scripts
.The OPs accepted answer might well work but it doesn’t work if you have the form setup to paginate using Ajax.
In that case I could only get it working using Javascript and the following method;
http://www.gravityhelp.com/documentation/page/Gform_post_render
You could of course use the
formId
parameter to limit this to a specific formThe currently accepted answer only works if the user never goes to a previous page in the form, if they do, then
gform_sourge_page_number
always lags by one. I found a better solution (using this hook for an example, but you should be able to use it within any hook that has the $form passed to it):GFFormDisplay::get_current_page( $form_id )
is one of many handy undocumented functions.I wrote a little function that returns the current page:
Further to the accepted answer, here is an example of finding the current page, and dynamically finding how many pages are in the form. This example changes button text based on whether it is the last page of the form, instead of enqueueing a script.
Following code works for me in JavaScript:
It will give you the page number of the current page in multi-page form.
Using Jquery
Try using this one:
Use the above method in the console to check how it’s working.