We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    Hi, I'm trying to execute a snippet after a form is submitted. The scenario is: a user submits a form, and a resource is created based on the input of the user. Because this involves taking a screenshot it takes some time. Because of this there is a few seconds loading time before the 'thank you' page is displayed after submitting the form.

    I would rather have this snippet execute after the form is submitted, confirmation email is sent and the user gets the thank you page, so he doesn't have to wait at all. I would like to have the resource creating/screenshot making happen 'in the background' without the user noticing it. Don't know if it's possible. There is no such thing as a hook for formIt that executes after the form is submitted and thank you page is displayed, is there?

    I'm using Revo 2.2.4 and formIt.

    How should I go about this, is it possible at all?
      • 1778
      • 659 Posts
      Hi,
      not sure of that but did you try to put your 'create resource/screenshot ' snippet directly on the 'Thank you' page, so your form is submitted, the email is sent, the 'Thank you' page is displayed and then, your snippet is doing its job...

      BTW, I don't know if there's a way to run it 'in the background', probably it's possible with a plugin, if you can find the correct system event...

      Hope this helps
      Cheers
        • 19328
        • 433 Posts
        Hi Anso, I just tried to put the snippet on the thank you page, and also as a hook on the formit call on the thank you page. It works (both ways), but when submitting the form you still have to wait 5 seconds before the thank you page is loaded. So it seems this doesn't make a difference.

        Yeah I thought of a plugin as well, but the 'onDocFormSave' event doesn't get fired if you create a resource with a formit hook (formit2resource) I think, because it's not working. Will have a look which event could be used. I have no idea if what I want is possible at all...
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          The only way you could have something like that be processed after the page has loaded would be to have an AJAX call on the page. http://api.jquery.com/ready/

          Have your processing snippet in a resource with no template, and use that resource's URL for the AJAX call. Just return "true" or "false", and don't handle the return in your AJAX unless it's false, then you could display an error message if you wanted to.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 19328
            • 433 Posts
            Ok, thanks, I'm going to try it with AJAX. Good to know that there is no other way.
            Thanks for pointing me in the right direction!
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Hmm. http://rtfm.modx.com/display/revolution20/OnWebPageComplete

              So you should be able to use a plugin using this event, and have the plugin code check for the ID of the resource that was processed and if it's the one with the form on it, do whatever you want done. The POST should still be available.

              No, further searching in the source code shows this does NOT occur after the response is sent, or at least not what I thought "the response is sent" means; it's referring to the return of a function with the response object. Sorry, I thought I'd found something.

              The post-processing takes care of final caching and invokes the OnWebPageComplete event. After that, the session is closed, the page is echo'd, and the write buffers are all flushed, then the whole thing exit's. (modresponse.class.php lines 175-193) [ed. note: sottwell last edited this post 14 years ago.]
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 19328
                • 433 Posts
                Looks interesting! But if I try to test this, something goes wrong: the check on 'onWebPageComplete' is not remembered. I check this event, save, then reopen the plugin, and the check is gone. Do you see this behavior as well? Or is something wrong with my installation...Very strange! It looks like this system event could be what I'm looking for, if only it would stay checked!
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  This won't do what you're looking for, as I had at first thought it might.

                  As far as why it won't stay checked, I have no idea.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 19328
                    • 433 Posts
                    Hi Susan, this might be a very stupid question, but I'm trying to load the snippet with ajax like you said. It's kind of working (snippet gets executed), but what I really can't figure out is if the ajax call should be placed on the thank you page or on the page with the form. I would think the thank you page is better, because the user won't have to wait for the process of creating the screenshot. But if I do that (and put the ajax call on document ready, I can't seem to get the post values anymore in the snippet. If I put the ajax call on the form page and execute it on submit, there still seems to be a long loading time which I am trying to get rid of. I'm still learning ajax/jquery so maybe I'm doing it wrong. So to at least get this right: on what page did you mean I should put the ajax call?

                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      Hm. Yes, that would be a problem. You may have to load the post values into the SESSION array, then have a snippet in the thank-you page to fetch the values; use the snippet to output the javascript code.
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org