PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Loading Gravity Forms dynamically with ajax

  1. jeroenlh
    Member

    Is it possible to load GForms dynamically with ajax, e.g. to use in a jQuery modal dialog?

    I understand and found information/examples for: hiding the form on the page and show it when needed in a modal dialog. Or how to implement a GForm in a jQuery dialog in a iframe.

    But what I really want is to load the GForm when needed using ajax.

    The idea is that the visitor can edit, delete, etc. some items on the page and when doing so, an ajax call will get the needed GForm for that action and then show it in a modal dialog.

    Is this possible with GF?
    Is there somewhere more info or documentation about usage like this?

    Thanx,
    Jeroen.

    Posted 12 years ago on Monday February 6, 2012 | Permalink
  2. Hi Jeroenlh,

    Calling and initializing a Gravity Form via an AJAX request is a fairly niche use case for Gravity Forms so there isn't much documentation on the subject. However, a good place to start is the parse_shortcode() function in gravityforms.php. You can see how it pulls the attributes from the shortcode and then uses these attributes to retrieve the form for display. Most notable is the get_form() function:

    [php]
    self::get_form($id, $title, $description, false, $field_value_array, $ajax, $tabindex)

    I can't say for sure what issues you will encounter with this approach but hopefully this will give you a decent start.

    Good luck!

    Posted 12 years ago on Saturday February 11, 2012 | Permalink
  3. jeroenlh
    Member

    Hi David,

    I had a look into the GF code and did some trails, but there seem to be to much javascript (initializing) problems when inserting the GForm into an existing page (using ajax).
    Not sure if it is me, or the GF architecture that makes this difficult.

    For this moment I will park this (wanted) solution and implement it by calling separate dialog pages te 'classic' way. I might give it another try later, when I have some more time.

    Posted 12 years ago on Tuesday February 14, 2012 | Permalink
  4. overit
    Member

    Hi All,

    I actually attempted the same thing recently using self::get_form. The form is displayed, but appears as it would in the admin area with the field titles and options visible and the fields themselves disabled. I believe it's because the IS_ADMIN constant is true when an ajax request is fired from a logged in user ( I'm not sure about logged out users ).

    In any event, I don't think it's quite possible yet. Anyone else had better luck?

    Posted 12 years ago on Thursday March 1, 2012 | Permalink
  5. Can you please post what you did

    Posted 12 years ago on Sunday March 18, 2012 | Permalink
  6. So, I noticed that the action for the form submission was the same as my AJAX query that loaded the page - not good. I couldn't figure out why this was happening so I decided to just fix it. I looked back at past sites using GF and saw that the form action should just be directed at the slug. Within the loop of the AJAX page, I added this jQuery/PHP function:

    <?php
    	 $root = get_bloginfo('url');
    	 $action = str_replace($root,'',get_permalink()); ?>
    	 <script>
    	 if ($('form').is('*')) {
    	 $('form').get(0).setAttribute('action', '<?php echo $action ?>')
    	 $('form').click(function(e) { e.stopPropagation();})};</script>

    This simply gets the permalink, removes the root url, and, if there is a form, inserts it as the form action. I also had a weird issue where anchor tags on page would get activated my clicking on the form, really weird. The stopPropagation function disables that, phew!

    It works.

    I have a could things to do to it.

    Posted 12 years ago on Sunday March 18, 2012 | Permalink
  7. paddelboot
    Member

    I am trying the same, and I cannot get past this IS_ADMIN constant either, which means I cannot fire AJAX calls the WordPress way. It might be a niche, still it would be nice to be considered in the Gravity Forms source code.

    Posted 12 years ago on Wednesday July 25, 2012 | Permalink
  8. Hi,
    I would also like to call and initialize a Gravity Form via an AJAX request. I only want to load the form and display it on the page/post when the user takes an action (e.g. clicking a link or button).

    I also took at stab at this by using the standard WordPress ajax functionality, but the form loads like it does in the WP Admin area as you say. For example in my theme's functions.php file I:

    A) Enqueued and localized the javascript.
    B) Created the "ajax" function to load the form that will called by the javascript.

    see http://pastebin.com/yUQEKa9k

    and then
    C) Created the appropriate javascript with the action trigger and ajax call to the form load function.

    see http://pastebin.com/fLy4apnJ

    Any help would be appreciated.

    Thanks.

    Posted 12 years ago on Monday November 5, 2012 | Permalink
  9. paddelboot
    Member

    I have only found one workaround:

    1. Consider this page template:
    `<?php
    /**
    * Load Gravity forms via AJAX
    *
    * Template Name: load form
    */

    // Display form
    gravity_form( your_form_id, FALSE, TRUE, FALSE, NULL, FALSE );
    exit;
    ?>`

    2. In the editor, create a new page using the above template. Let's say you title this page 'My ajax form'. After saving, you can fire an AJAX call (using WP's AJAX implementation) for this page's url (i.e. example.org/my-ajax-form/). This call should return just the form.

    Let me know if it works for you,

    Paddelboot

    Posted 12 years ago on Wednesday November 7, 2012 | Permalink
  10. Paddleboot, could you please explain the below further?

    After saving, you can fire an AJAX call (using WP's AJAX implementation) for this page's url (i.e. example.org/my-ajax-form/).

    many thanks,
    Andy

    Posted 11 years ago on Friday December 14, 2012 | Permalink