We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25586
    • 105 Posts
    I have a quick question. I’m working with ajax to get the content of a page. I’ve followed this tutorial http://tutorialzine.com/2009/09/simple-ajax-website-jquery/ and that is working. But how can I get the load-page.php connect with the modx database? I think I have to include some files but I don’t know which one.

    Another question pops into my head: How can I get a snippit to work with a ajax call? Do I need to create the document with the snippit call? Or can I just get the data out of the database and prosses it in some way?

    I hope I wrote clearly, English is not my native language.

    Best regards,

    Rogier
      • 11076
      • 159 Posts
      There was a guide on venugopal’s blog but the site was hacked as i see now, here’s a copy with the kind help of Google’s cache:
      http://webcache.googleusercontent.com/search?q=cache:a7YFVuw6v2QJ:venugopalreddy.com/category/modx/+venugopalreddy.com/+ajax&cd=1&hl=iw&ct=clnk&gl=il&source=www.google.co.il

      I Copy&Pasted it for your convinence, it should help in general:

      Modx Revolution + Ajax ( by venugopal )
      Lot of people asked in the forum how to use ajax in modx with processros,connectors bloblobl
      like the core developers said dont use connector for fontend ajax functionality
      instead refer a plain dcument for ajax calls
      let me tell you how to use this
      create a snippet in the backend and name it ajax
      copy the code below ,self explanation code with comments as usual dont worry about the $_REQUEST variables now
      <?php
      $output='';
      //this is the processor file name
      $procname=$_REQUEST['action'];
       
      //this is what component we are referencing
      $comp=$_REQUEST['component'];
       
      //get the processor file with path
      $f= $modx->getOption('core_path').'components/'.$comp.'/processors/'.$procname.'.php';
      if (file_exists($f)) {
      //include that file so the output of the processor is assigned here
                  $output= include $f;
              } else {
                  $output= 'Action not found: '.$f;
              }
      //convert the processor output to json format
      return $modx->toJSON($output);

      now create a document with Ajax name and Alias as ajax
      select empty as template check hide from menus and publish it
      in the content window call our ajax snippet as not cache-able
      [[!ajax]]


      guess what we are done with ajax functionality
      all you need to do is pass the action,component variables in your javascript
      for any ajax request in your site the url for your ajax request is yoursite/ajax if you enabled SEF
      otherwise url is yoursite/index.php?id=ajaxdocumentid

      for example your submitting form as ajaxsubmit
      if your processor is addpoll.php in components/vxpolls/processors/web/addpoll.php
      than action is web/addpoll
      component is vxpolls
      in the form add this variables as hidden inputs
      <form id="myForm" action="ajax">
      <input type="hidden" name="action" value="web/addpoll" />
      <input type="hidden" name="component" value="vxpolls" />
      some other form elements
      </form>

      in my case iam using mootools ajavascript so i need to call $(‘myForm’).send();
      so now my form is sent to ajax document(see action of the form)
      the ajax page will send the form to core/components/vxpolls/web/addpoll.php
      simple right

      lets take another example where you want to get poll data from backend/php with ajax iam assuming you are returning json data
      var jsonRequest = new Request.JSON({url: 'ajax', onSuccess: function(responce){
          alert(responce.pollname)
      }}).get({'action': 'web/getpoll', 'component': 'vxpolls'});

      all js frameworks support this kind of ajax functionality, sending variables to ajax url
      so dont need to worry if your not using mootools

      i will tell you when and how to use connectors later
      remember connectors are better suits for backend not for frontend

      happy coding
        Michael Shraibman( gOmp)  | Freelance Design & Development | wink  impossible is nothing...
        • 25586
        • 105 Posts
        Thanks!

        I will take a look at it!

        Regards,

        Rogier
          • 18373 ☆ A M B ☆
          • 3,141 Posts
          If you’re going to use that snippet, please do sanitize your variables... rolleyes
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • 11076
            • 159 Posts
            Quote from: Mark at Feb 23, 2011, 08:57 AM

            If you’re going to use that snippet, please do sanitize your variables... rolleyes
            it’s important to double check security vulnerability.
            it’s a good hint why the original blog is now down laugh
              Michael Shraibman( gOmp)  | Freelance Design & Development | wink  impossible is nothing...