Skip to content

Auto-Submit a Form on Page Load Gravity Forms

Chris Eggleston 1 min read

We have had a couple of requests from Gravity Forms users that wanted to know how to submit a form automatically once the form page loads.

In some cases, they are passing data via a query string and all they need is for that data to populate the form and be submitted.

Use Case Example

One use case I can think of is a simple solution for employees to clock in and out. They log in to their account, and click the “clock in” button which is preloaded with a URL and any necessary parameters. I’ve demonstrated this below.

To get this to work you only need an HTML field in your form and the code snippet below.

Setting Up Your Form

You can add whatever fields you’d like to your form, but for this to work, you must have an HTML field with the code below added to the content field.

Below is the code snippet you will need.

To make the code work you will need to make two modifications after pasting it into your HTML field.

  1. Change ID# on line 24 to your forms actual ID number
  2. Change the delay time on line 27, currently set to 500 which is half a second
form-auto-submit.jsGist by chriseggGitHub
/**
* GravityRanger Auto-Submit Form on Page Load
*
* Allows you to auto-submit a form when the page loads, with a 1/2 second delay.
* https://gravityranger.com/auto-submit-form-page-load/
* 
* @instructions:
* 
*  1. Change gform_ID# to your forms ID number (line 24)
*  2. Set the amount of time you'd like to delay the script (line 27)
*  3. Add the scipt to an HTML field within your form
* 
* @version  1.0
* @author   GravityRanger
* @license  GPL-2.0+
* @link     https://gravityranger.com/
*
*
*/

<script type="text/javascript">
setTimeout(function(){
jQuery(document).ready(function(){
    jQuery('form#gform_ID#').trigger('submit'); //change ID to your forms ID# number
});

}, 500); //seconds before submit (1000 = 1 second)

</script>

Additional Code Snippet

If you prefer to not have a delay in your submission and instead have the form submitted as soon as the form renders, you could use the snippet below.

Just be sure to replace ID# with your forms ID number.

<script type="text/javascript">
     jQuery(document).on('gform_post_render', function(){
           jQuery('.gform_wrapper:not(.gform_validation_error) form#gform_ID#').trigger('submit');
    });
</script>

The Wrap Up

I’m sure there are many other use cases where having the ability to auto-submit a Gravity Forms form is necessary, and if you have one of those cases, hopefully, this solution will work for you!

If you have any questions or need additional assistance, please use the comments below.

Comments

  1. Marky

    Thank you for info.

    by the way, how to put conditional logic?

    For Example, submit button is not shown based on the conditional logic then pause or stop auto-submit.

    is there an example can see and try?

  2. Norman

    Frickin’ cool!!

    My only comment is clarification on the syntax regarding the form ID. Here’s my working code:

    1. Chris Eggleston

      Hey Norman,

      Your code was not included with your comment…

      1. Norman

        I thought I put it in there — I just replied to my post with the code (again?). In any case, I used the “additional code snippet” from above and used the substitution below. Per the directions above, I literally substituted the characters “ID#” with my form number “10”, resulting in the look below.(Perhaps it was just me, but it wasn’t exactly obvious to me what exactly should be replaced.)

        This really is frickin’ cool! Thanks, Chris!

        BTW, I’m considering removing the confirmation webpage notification once the form is automatically submitted — I can’t find how to disable or delete the confirmation. Any ideas?

        ‘form#gform_10’

        1. Chris Eggleston

          Sorry, WordPress must be stripping the code when you publish your comment. Maybe a setting I need to sort out.

          At any rate. When adding your form ID you would replace ID# with your forms actual ID number as you demonstrated above.

          You can’t delete the default confirmation message or disable all confirmations, every form requires at least one confirmation.

          You could set the redirect to take the user back to the initial page (or any page), in my example that would be the page where they click clock in or clock out.

    2. Norman

      OK, the code doesn’t show when I copy/pasted, please delete this post and edit my original to show my example above if you don’t mind.

    1. Chris Eggleston

      Hi Russell, which code snippet did you try, delay no delay? And did you add your form ID in place for ID#?

      1. Russell

        Hi,

        Yeah I tried both and amended the form id. I have some required fields on my form but i expected just to see the rejection message to say the required fields haven;t been completed (as if i had manually clicked the submit)

  3. Doug H

    Hello, I’m wondering if you are still able to offer any assistance on the auto-submit code. I was not able to make either one work. Both sets of code were set up as instructed but the form did not auto submit in either case. I was able to manually submit in each case. Are there any adjustments for new versions of Gravity that might need to be made?

    1. Chris Eggleston

      Hi Doug,

      These snippets should still work with the current version. Are you placing the code in an HTML field within the form? And you added your form ID to gform_ID# like this gform_12 (where 12 is the ID number)?

      What is your use case for the snippet?

      1. Doug H

        Thank you for the reply… I’m not sure why it wasn’t working for me at first but I got it going after matching the code exactly to the snippet. It works perfectly. Thank you!

Ask a question or leave a comment

Your email is never shown. For help with a specific form, share what you expected and what happened instead.