I'm trying to append a gravity form after a certain choice is made. I've a couple choices a visitor can make, and based on that choice I want to load a Gravity form via Ajax.
I've managed to create a successful AJAX call and receive the Gravity form as html. But whenever I try to append this to a div, it tells me gform is not defined. I guess it's trying to load the form.
const dealerForm = jQuery('.js-dealer-form');
jQuery.ajax({
type: 'GET',
url: ajax.url,
data: {
action: 'load_gravity_form',
formid: id,
formtitle: 'true',
formdescr: 'true',
formajax: 'true',
},
}).done(function(data) {
dealerForm.html(data);
});
// Get js file from manifest
$manifest = json_decode(file_get_contents(get_template_directory() . '/dist/rev-
manifest.json'), true);
$jsFile = $manifest['app.bundle.full.js'];
wp_enqueue_script( 'ajax-js', get_template_directory_uri() . '/dist/scripts/' .
$jsFile, array('jquery') );
wp_localize_script( 'ajax-js', 'ajax', array(
'url' => admin_url( 'admin-ajax.php' )
));
// Add the action and nopriv action to account for users not logged in
add_action( 'wp_ajax_nopriv_load_gravity_form', 'load_gravity_form' );
add_action( 'wp_ajax_load_gravity_form', 'load_gravity_form' );
function load_gravity_form() {
// Return the following:
// The form ID
$gfId = intval($_GET['formid']);
$gfTitle = filter_var($_GET['formtitle'], FILTER_VALIDATE_BOOLEAN);
$gfDesc = filter_var($_GET['formdescr'], FILTER_VALIDATE_BOOLEAN);
$gfAjax = filter_var($_GET['formajax'], FILTER_VALIDATE_BOOLEAN);
// This is the shortcode to embed the form
gravity_form($gfId, $gfTitle, $gfDesc, false, '', $gfAjax, 1);
die();
}
$forms = GFAPI::get_forms();
foreach ($forms as $form) {
gravity_form_enqueue_scripts( $form['id'], true );
}
$GLOBALS['timberContext'] = \Timber\Timber::get_context();
ob_start();
When I place
{{ function('do_shortcode', '[gravityform id="25" title="false" description="true" ajax="true"]') }} in my template, gform is not longer not defined. But that should not be the right solution I guess.
Any ideas?