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

    I’m trying to overwrite the MODx output with something else to build a AJAX Snippet. Here’s the basic idea:
    Snippet creates a form that is displayed inside a document. When the user submits the form, a ajax request is being sent. What i’m trying to achieve is, that the same snippet detects the ajax request and returns a response instead of the normal document output. I already tried to modify $modx->documentOutput inside the snippet, but that didn’t work.
    I guess this should be no problem when implemented as Plugin, but is there a way to make this work with a snippet? Or should i choose a Snippet/Plugin combo?

    Any input highly appreciated. Thanks.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      The whole point of AJAX is that the javascript retrieves the response and writes directly to the web page as it is in the browser. AJAX requests will not reload the document, that’s why it’s used.
        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
        • 2734
        • 165 Posts
        Hi sottwell

        Yes, i understand the concepts of AJAX. Maybe i did not make myself clear:

        • Document is created normally. Snippet creates a form as output
        • User presses submit button. Form values are sent via HttpRequest Object (AJAX)
        • Now the important part: The values are sent to the exact same URL, thus, modx will "generate" the requested document once more
        • Upon snipped execution i check if the snippet is called via AJAX Request and i’d like to return some custom values instead of the normal document output


        This means, that i’m not reloading the document, i’d like to get a custom response from a document when called via AJAX. Actually, the MODx output should stop before any HTML content is written and return just the AJAX response instead of generating the document as usual.
        Hope that clears things up?

        Edit:
        Hm. Echoing my output and then calling "exit();" seems to do just what i want... Don’t know if that is the preferred way to do such a thing though. I was "inspired" by the $modx->sendErrorPage(); Method which simply exits after execution.
        I’ll keep you posted if my snippet works as intended. If yes, this could be a very nice way to integrate ajax requests/responses with just one snippet.
          • 33337
          • 3,975 Posts
          umm, I don’t have personal experience in stuff like this, but I would like to take your attention to index-ajax.php, it might be more helpful.
            Zaigham R - MODX Professional | Skype | Email | Twitter

            Digging the interwebs for #MODX gems and bringing it to you. modx.link
            • 2734
            • 165 Posts
            Quote from: zi at Jan 29, 2008, 07:39 AM

            umm, I don’t have personal experience in stuff like this, but I would like to take your attention to index-ajax.php, it might be more helpful.
            Thanks for the hint!
            I looked at the index-ajax.php file. It seems to allow inclusion of a php asset without the hassle of including modx config manually. Still, i like my approach better because all could be handled within just one snippet. Using index-ajax.php would surely cause less overhead (no initialization of the parser etc.), but other than that?