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

    my ajax functions don’t recognize $modx even though i’ve already included global $modx prior to method calls.

    the same functions work normally outside of ajax calls.

    please help!

    thanks.

      • 22303 MODX Staff
      • 10,725 Posts
      How are you implementing your Ajax resources? I implement them as MODx documents personally; that way you have all of the MODx resources you need. Others have chosen to use external PHP scripts, in which case, you’ll need to include all the necessary things to get the DocumentParser class instantiated so you can use it. This can be done, but I recommend creating Ajax scripts as snippets/pages in MODx.
        • 33480
        • 27 Posts
        Quote from: OpenGeek at May 29, 2008, 09:30 AM

        How are you implementing your Ajax resources? I implement them as MODx documents personally; that way you have all of the MODx resources you need. Others have chosen to use external PHP scripts, in which case, you’ll need to include all the necessary things to get the DocumentParser class instantiated so you can use it. This can be done, but I recommend creating Ajax scripts as snippets/pages in MODx.

        Yes I used external scripts, and I think I never instantiated DocumentParser (i don’t know how to do it too in this case).

        i’ll try your suggestion.

        does that mean on the javascript, i’ll refer to the modx page as:

        index.php?id=1

        1 here as an example refering to the ajax page, and then trail this with whatever parameter?
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: zerogravity at May 30, 2008, 10:04 AM

          Yes I used external scripts, and I think I never instantiated DocumentParser (i don’t know how to do it too in this case).
          You can do so very easily:
          <?php
          define('MODX_API_MODE', true);
          include('/path/to/index.php');
          
          // now use $modx freely, though in it's current state 
          //    there are some limitations to this method.
          ?>


          Quote from: zerogravity at May 30, 2008, 10:04 AM

          i’ll try your suggestion.

          does that mean on the javascript, i’ll refer to the modx page as:

          index.php?id=1

          1 here as an example refering to the ajax page, and then trail this with whatever parameter?
          That really depends on what kind of Ajax request/response you are performing, but you should be able to use MODx tags to build the links directly in the Ajax document’s content in most cases, e.g. [~1~] or [~[*id*]~] for the current page, except where that is needed in external JS. In those cases, I try to pass such variables as JSON configuration objects into the external JS objects (typically by passing the configuration object through function parameters) from the document.

          Hope that helps a little.