We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42101
    • 125 Posts
    Hi!

    I was wondering how to use Ajax to call some data on a website without a page Reload. I was searching the Forum but only found posts from 2005...

    I'd like to know how to make a "FormIt" or any other Form submission with Ajax to get a success message while the form was submitted successfully? All without Page reload?

    I really have no clue where to start. Has anybody Modx tutorials with working Ajax examples? I know the Gallery Tutorial in the migx section, but it doesn't really makes clear how to start with the formit stuff....

    thank you!

    AD
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Use a resource with no template as your back-end processor. Put the snippet you want to run in that resource. Use the URL to that resource in your AJAX request.
        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
        • 3109 ☆ A M B ☆
        • 894 Posts
        I'm actually working on doing this right now for my new site but I have resorted to do all the validation via JS and have the regular FormIt validation as a fallback for people with JS disabled.

        @Susan can you elaborate what you mean? FormIt handles all the stuff so I am not using any snippet so I would like to know about your method to see if there is anything I can do better.

        Thanks.
          Benjamin Marte
          Interactive Media Developer
          Follow Me on Twitter | Visit my site | Learn MODX
          • 42101
          • 125 Posts
          Quote from: benmarte at May 01, 2013, 10:20 AM
          I'm actually working on doing this right now for my new site but I have resorted to do all the validation via JS and have the regular FormIt validation as a fallback for people with JS disabled.

          @Susan can you elaborate what you mean? FormIt handles all the stuff so I am not using any snippet so I would like to know about your method to see if there is anything I can do better.

          Thanks.

          Hi benmarte!

          Yes, the validation works with js but I'd like to get something like "Thank you for your Message" after Clicking the "send button"

          At the moment the formIt redirects to another page with the "Thank you for your Message" field visible. That is not a nice solution - it works. But I'd like to have it after the send button is hit and the message was really sent successfully.

          @susan
          thanks for the info. I will try it out.


          AD
            • 3109 ☆ A M B ☆
            • 894 Posts
            You need to use ajax success method in order to show the success message if the form was successfully submitted, you also need to prevetnDefault the submit buttons click function that way the page doesn't go to your success message page unless the user has JS disabled.

            Something like this:
            $.ajax({
                        type: "POST",
                        url: this.url,
                        data: form_data,
                        beforeSend: function() {
                        //show this while sending
                        },
                        error: function() {
                        //display error message here
                        },
                        success: function() {
                        //display success message here
                        },
                        complete: function() {
                        //do this after form is done submitting
                        }
                        });
            


            Good luck.
              Benjamin Marte
              Interactive Media Developer
              Follow Me on Twitter | Visit my site | Learn MODX
              • 38290
              • 712 Posts
              It's true there aren't enough forum posts on AJAX. This concept isn't relevant to form submissions so don't let it distract you but I've been curious what other MODX-AJAXers think of this 'AJAX Forking' method of mine to allow AJAX representations of resources to be served from that resource directly, rather than through some sort of API router.
              http://devries.jp/blog/2013/04/10/create-a-map-of-ski-resorts-with-modx/#ajaxforking
                jpdevries
                • 42101
                • 125 Posts
                Quote from: dinocorn at May 01, 2013, 02:52 PM
                It's true there aren't enough forum posts on AJAX. This concept isn't relevant to form submissions so don't let it distract you but I've been curious what other MODX-AJAXers think of this 'AJAX Forking' method of mine to allow AJAX representations of resources to be served from that resource directly, rather than through some sort of API router.
                http://devries.jp/blog/2013/04/10/create-a-map-of-ski-resorts-with-modx/#ajaxforking

                Great Source! Thank you!

                I really was searching for tutorials and stuff for a while, but it's really not that easy to find examples and stuff.
                I wil read it.

                @benmarte
                Great - thank you for the code. I will dig into into it.

                I am just starting with Ajax and Modx - I was working with Wordpress before and there it was quite easy to call Ajax. At the moment it takes some time to dig in it.

                A super easy step by step tutorial would also be great. I just found some more or less tricky once to start with - but not for modx noobs like I am. smiley


                Thank you all,

                AD
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  Actually it's very easy to implement AJAX in MODx.

                  1. Create a snippet to generate whatever output you want to load with your AJAX request, if you need back-end PHP processing. It goes on a resource with no template. Or, if you're using JQuery's load() function, have the desired output in a specific HTML element; the load() function takes a parameter to indicate which HTML element you want to insert. So you can use one resource to handle a number of AJAX requests.

                  2. Use that resource's URL as the AJAX request's URL.

                  That's all, as far as MODx is concerned. Creating the triggers and setting up the structure is up to the developer.
                    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
                    • 3109 ☆ A M B ☆
                    • 894 Posts
                    @Susan Yeah the problem with that is that FormIt handles the error/success messages and these don't exist until you refresh the page since MODX needs to parse them.

                    From the sound of it, it seems in order to use Ajax with MODX to submit forms you would need to create a custom validation snippet thus negating the use of FormIt or end up doing all the validation in JS and use the FormIt validation as a fallback for users with JS disabled.
                      Benjamin Marte
                      Interactive Media Developer
                      Follow Me on Twitter | Visit my site | Learn MODX
                      • 4172
                      • 5,888 Posts
                      I'm very sure, its possible to let formit, may be wrapped in a custom-snippet do the validation on the ajax-page and return a success- or error- json-responce.

                      you can use the formit-placeholders to fill-out the json-response.
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!