We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1778
    • 659 Posts
    Brilliant ! laugh Thanks Shaun...
    Once again your answers are tremendously helpful
    Cheers
      • 27519
      • 275 Posts
      Thanks! I think I get the picture...

      You’ve been a big help... again!
        MODx Revolution / MAMP / OS X
        • 27519
        • 275 Posts
        Did some more digging around on how to get that $_SESSION variable into my Javascript environment. Found two options:

        1: Via a JSON encode:

        <?php 
            return json_encode($_SESSION['msg']);
            ....
        


        2. Via an AJAX call:

        Javascript side:
        $.ajax({
              url: "test.php",
              cache: false,
              success: function(html){
                eval( html );
              }
            });
        


        PHP side test.php:
        echo 'var myData = "'. $_SESSION['msg'].'"';
        


        Any opinions on this? Are both applicable to MODx manager environment?
          MODx Revolution / MAMP / OS X
          • 28215
          • 4,149 Posts
          I recommend never using eval.

          That said, how I’d do it is in my controller, I’d use $modx->regClientStartupHTMLBlock before instantiating my JS object:

          $content = $_SESSION[’msg’];
          $modx->regClientStartupHTMLBlock(’<script type="text/javascript">
          var Tarriffs.currentContent = "’.$content.’";
          </script>’);

          And then reference Tarriffs.currentContent in my JS config in the panel.

          Note: you might need to escape apostrophes in your $content var.
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 27519
            • 275 Posts
            Ahh..... cool Excellent tip!

            Thanks!

              MODx Revolution / MAMP / OS X