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

    I was wondering whether there is a way to execute a snippet through an AJAX call?

    Thank you in advance for any hints how this could be done.
      • 22303 MODX Staff
      • 10,725 Posts
      At the moment, you can create the Ajax page as a MODx document, you could try using the MODxAPI add-on in the repository, or you could attempt including the index.php file which will instantiate the main MODx class and make it’s functions available to you. Creating and managing the Ajax resource as a MODx document would be my preference, and I’ve done this for several projects with no problems.

      Future releases will include that ability to easily do this by simply including the main MODx class, looking up a snippet object, and processing it.
        • 19889
        • 616 Posts
        actually, it’s just the following snippet that I like to call using AJAX - I was wondering whether I could put it into the document and call it from within a different page using AJAX.

        $ticker = isset($ticker) ? $ticker : 'MSFT';
        $fd = fopen ("http://quote.yahoo.com/d/quotes.csv?s=$ticker&f=sl1d1t1c1ohgv&e=.csv", "r");
        $data = fgetcsv ($fd, 1000,",");
          // data[0] = symbol
          // data[1] = last price
          // data[2] = date
          // data[3] = time
          // data[4] = change
          // data[5] = open price
          // data[6] = day high price
          // data[7] = day low price
          // data[8] = trade volume
          //close the filehandle $fp
        
        fclose ($fd);
         
        echo "<p>$data[0] stock tracker</p><p>Most recent trade: <strong>$data[1]</strong></p><p>Today's Change: $data[4]</p><p>(Prices delayed up to 20 minutes.)</p>";
        
          • 22303 MODX Staff
          • 10,725 Posts
          Yep, just create a document with no template and that snippet call as the content, then reference the link to that document, e.g. [~13~] if it’s id is 13, in your Ajax script responsible for loading the data.
            • 19889
            • 616 Posts
            cool - thank you

            well, I’m still kind of lost with this;

            on the main page I basically have this code:
            	indices: function() {
            		var accordion = new Accordion('dt.toggler', 'dd.section', {
            			opacity: false,
            			fixedHeight: 170,
            			onActive: function(toggler, element){
            				toggler.setStyle('color', '#ff3300');
            				var url = "http://localhost/sites/trade.ch/index.php?id=137&ticker=^DJA";
            				new Ajax(url, {
            					method: 'get',
            					update: toggler.getNext()					
            				}).request();
            			},
            		 
            			onBackground: function(toggler, element){
            				toggler.setStyle('color', '#222');
            			}
            		}, $('accordion'));
            	}	
            


            then there is page 137 that has just a snippet call like this: [[stocks]] - the main question now is how I can pass the ticker symbol to the snippet through page 137 - another issue is whether I can use [~137~] in the javascript code above instead of having to put the full url.

            Maybe it’s easier to just put the php code into an external file instead of having it as a snippet.

            Hope you can help - Thank you in advance for your input!
              • 7885
              • 40 Posts
              please read this document, this clarifies a way to work in MODx using ajax

              http://wiki.modxcms.com/index.php/Use_AJAX_with_modxAPI
                visit us at www.dospuntocero.cl