Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

519
Views
gform not defined after loading gravity form via AJAX

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.

JS

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

functions.php

// 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();

}

header.php

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

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!