We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8425
    • 159 Posts
    Hi,
    I'm trying to pass a parameter to a snippet in Javascript but it does not work. This is what I'm doing. I have a javascript function that passes a document ID as parameter. I want to pass that document id to a snippet as follow:
    <script>
    function getPageContent(pageID) {
    var $j = jQuery.noConflict();
         $j(document).ready(function(){
             
             // Works fine
             $j('.serviceImg').append('[[GetField? &docid=`51` &field=`article_img`]]');
    
            // Does not work 
            $j('.serviceImg').append('[[GetField? &docid=`pageID` &field=`article_img`]]');
            // Does not work either
            $j('.serviceImg').append('[[GetField? &docid=`' +pageID+ '` &field=`article_img`]]');
          
          });
    }

    Does anyone know how I could pass pageID to the snippet within Javascript above?
    Thanks a bunch in advance for your time adn help
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      What is the pageId? Is it the ID of the current page?

      You can use a snippet to generate the javascript, setting the desired pageId with the snippet code, and the snippet's output would be the javascript; or you could use the MODx API regClientScript() function to load the javascript, depending on where you want the javascript tags to appear.
        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
        • 8425
        • 159 Posts
        Yes, pageId is actually a the ID of the selected document. I execute this piece of code upon menu selection.
        How can I use a snippet to generate the js? I don't quite understand. The function getPageContent(pageID) is invoked when a user clicks on a menu item. Then I want to populate the page without reloading it. My problem is that I can see my pageID (alert(pageID) returns the correct page id) but I cannot use it as an argument for a snippet such as [!MySnippet? id=`pageID` !] from within the js.
        Let me know if it's not clear. Thanks for helping me out here...
          • 8425
          • 159 Posts
          Ok, I've made some progress. I invoke a JQuery.getJSON as follow:
           jQuery.getJSON('[(base_url)]populatePage.php?docID='+pageID, function(data) { 
                    var items = data;
                    });

          But when I get the array back, it is undefined and empty. This is how my populatePage.php looks like. What am I doing wrong?
          <?php
          
          $docId = $_GET['docID'];
          
          $path = dirname(__FILE__).'/';
          include_once($path.'modxapi.php');
          
          $modx = new MODxAPI();
          
          $img = $modx->runSnippet('GetField', array('docid'=>$_GET['docID'], 'field'=>'article_img'));
          $desc = $modx->runSnippet('GetField', array('docid'=>$_GET['docID'], 'field'=>'image_desc'));
          $title = $modx->runSnippet('GetField', array('docid'=>$_GET['docID'], 'field'=>'pagetitle'));
          $text = $modx->runSnippet('GetField', array('docid'=>$_GET['docID'], 'field'=>'article_text'));
          
          $result = array(
            'img' => $img,
            'desc' => $desc,
            'title' => $title,
            'text' => $text
          );
          
          echo $result;
          ?>

          Thanks for helping out as I am stuck....
            • 33968
            • 863 Posts
            You're calling getJSON but what you're getting is not JSON, it's just an array.

            Try wrapping your output like this:
            return $modx->toJSON($result);
            

            It looks like you're using Evo so I'm not sure if that's the correct method to use, perhaps Susan can shed some light on that
              • 8425
              • 159 Posts
              I think you're correct. I tried to use
              json_encode($result)
              but it doesn't work in php 4.4.9. I need to ask my provider to upgrade to 5.2.
              Thanks