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.

Javascript alert when leaving multiple page form

  1. greg
    Member

    First of all, I'd like to say thank you for making Gravity Forms. I've been using it for more than a year, and can honestly say it is one of the best WordPress plugins.

    Here's an issue I'd be grateful to have some insight on:

    I'm building a multiple page form. I would like to have a javascript alert to confirm that a user would like to leave the form page before submitting the form. This alert would need to display when the user closes the browser tab, clicks on a link to go to another page within the same tab, closes their browser application, etc. In other words, I just want to ask the user "Are you sure you want to leave this page before submitting the form?"

    I'm no expert when it comes to javascript, but I've done a bit of Googling/RTFM'ing on this subject, and have come across quite a few example code snippets, such as:

    http://www.openjs.com/scripts/events/exit_confirmation.php

    I want to double check that using the above javascript (or something similar) will not conflict with any javascript that the Gravity Forms plugin is running. My Wordpress site's theme is Thesis, so it's trivial to paste in some javascript specifically for the form page.

    Is there a better way to do this type of alert, as opposed to the script I linked to above? I know jQuery is great for cross browser compatibility, but I don't want to run into any conflicts with Gravity's js...

    Thanks in advance for your help and advice.

    Posted 13 years ago on Thursday December 1, 2011 | Permalink
  2. You should be able to use standard javascript or jQuery to accomplish this. Gravity Forms outputs standard HTML so customizations can be implemented using jQuery. So this should be doable.

    I would suggest playing around with it on a test form or a test site and see if you can get it to work. It certainly can't hurt to experiment and try.

    What you describe may make a good built in feature as a form option. If a user has started filling out a form and then tries to navigate away without submitting it, display this type of alert. I'll note this feature idea for future reference.

    Posted 13 years ago on Thursday December 1, 2011 | Permalink
  3. greg
    Member

    Thank you for your reply, Carl.

    Unfortunately, I haven't had any luck with the script I linked to above. I did try another script (the jQuery script below), which seems to work fine in Safari, but doesn't work at all in Firefox, Opera, or IE.

    <script type="text/javascript">
    	$('#reload').click(function() {
    	 	location.reload();
    	});
    	$(window).bind('beforeunload', function(){
    		return '>>>>>Before You Go<<<<<<<< \n Your custom message go here';
    	});
    </script>

    I'm stuck (from a code perspective), and would greatly appreciate some advice on moving forward with this challenge.

    I think having this as a built-in form option in future releases would be very valuable. It's frustrating for a user to be half way done with a multi-page form and lose their data, because they accidentally clicked a link or closed the window/tab.

    For what it's worth, I believe a nearly identical feature is already built in to the back end admin panel of Gravity Forms. When editing a form, a confirm dialog is displayed when trying to exit/refresh the page before hitting the "Update Form" button--but the confirm dialog is not displayed when the "Update Form" button has been submitted. This seems to be exactly what I'm trying to do, only on the front end...

    Can you point me in the right direction on how I can make this work, please?

    Posted 13 years ago on Friday December 2, 2011 | Permalink
  4. Can you post a link to your form please so we can see what's going on? Thank you.

    Posted 13 years ago on Sunday December 4, 2011 | Permalink
  5. greg
    Member

    Thank you for your reply, Chris.

    I'll be glad to provide a link to the form for support purposes. Some of the questions on the form are proprietary, though, so I have the forms on password protected pages.

    Would it be possible to send a link and login information to you via email?

    Posted 13 years ago on Monday December 5, 2011 | Permalink
  6. Sure, please sent an email to chris@rocketgenius.com and I will take a look.

    Posted 13 years ago on Monday December 5, 2011 | Permalink
  7. greg
    Member

    Hi Chris,

    I just wanted to make sure you received my email on Monday, and that you were able to access the forms.

    Thanks.

    Posted 13 years ago on Wednesday December 7, 2011 | Permalink
  8. Greg, you asked earlier how Gravity Forms does it in the form editor. Here's the code for that:

    [js]
    jQuery(document).ready(function() {
        gforms_original_json = jQuery.toJSON(form);
    
        window.onbeforeunload = function(){
            UpdateFormObject();
            if (gforms_original_json != jQuery.toJSON(form)){
                return "You have unsaved changes.";
            }
        }
    });

    This is present in the plugin in js/form_editor.js. It checks to see if the original JSON is the same as what is currently present in the page (i.e. if there were any changes in the editor) and if there were, pop up the dialog that says "You have unsaved changes." on line one, and the browser default message on line 2.

    I believe that code will work for you (with modifications maybe), but you need to have that code present on all pages of a multi-page form? For that, you can use the gform_page_loaded hook.

    You also mentioned that it was only working in Safari. Can you explain what happens in the other browsers, or post a link to a form online where you have implemented this, so we can check it for errors? You can email me again if you like chris@rocketgenius.com - thanks.

    Posted 13 years ago on Sunday December 11, 2011 | Permalink
  9. greg
    Member

    Thank you for your help, Chris. I've added the code from your last forum post into a Wordpress page containing a test form, which is a single page. The script generates the following alert message when I use Safari:

    "Are you sure you want to reload this page? You have entered text on this form. If you reload the page, your changes will be lost. Do you want to reload the page anyway?"
    The alert has options to either cancel or reload.

    That's exactly what I was hoping for. But in Safari, the alert message is only being displayed some of the time. I've played around with the test form quite a bit, and most of the time the alert will be displayed if two or more fields are filled in. But some times it won't display the alert when it should.

    I looked at the error console in the Safari web inspector, and it's displaying two errors:

    ReferenceError: Can't find variable: jQuery
    Failed to load resource: Could not connect to the server.

    The script you posted isn't working at all in Firefox or Opera on OSX. I can reload the page or close the tab without any alert message being displayed at all, even if all of the questions on the test form are filled out. I've also loaded the test form in Windows XP, using Firefox and IE8, and wasn't able to get the alert message to display in those browsers.

    The error console for Opera displays one error (which appears to be the same error as is being displayed in Safari's console):

    Uncaught exception: ReferenceError: Undefined variable: jQuery

    So, I feel like we're close to getting this issue worked out, but the previously mentioned errors in the script are preventing it from working as we intended. I'm hopeful that if we debug these errors, the script will work--at least on a single page form.

    The link to the test form is below, if you'd like to take a look at it:

    All Flooring Inspections

    You can use the same login information I sent to you via email to access the above test form.

    I also looked at the gform_page_loaded hook that you mentioned in your last post. Can you explain how I should include that in the multi-page forms where I need the alert to show up, please?

    Thanks again for your help!

    Posted 13 years ago on Friday December 16, 2011 | Permalink
  10. greg
    Member

    It's been a couple of weeks since my last post. I realize everyone was busy over the holiday season, but I'm curious if you guys have any additional information on resolving this issue. Any ideas, help, or advice would be appreciated.

    Thanks.

    Posted 12 years ago on Wednesday January 4, 2012 | Permalink
  11. greg
    Member

    I want to follow up again on this conversation, because I never got a response to my last two posts.

    Is this an issue you can provide support on?

    Posted 12 years ago on Wednesday February 8, 2012 | Permalink

This topic has been resolved and has been closed to new replies.