We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    Whoops, just deleted my post.

    Just as a point of discussion (and because I'm working on the WordPress vs. MODX book), it's interesting to look at how WordPress handles this -- it's similar ... check out http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/

    Basically, the trick is passing PHP variables to Javascript. WordPress does this via the wp_localize_script() script, which ends up printing out a Javascript block in your HTML head.

    <?php
    $data = array();
    $data['stuff']
    $data['ajax_url'] = admin_url( 'admin-ajax.php' );
    wp_localize_script( 'id_of_script', 'my_var', $data );
    ?>


    Creates this:

    <script type='text/javascript'>
    /* <![CDATA[ */
    var my_var = {"stuff":"value", "ajax_url":http:\/\/yoursite.com\/wp-admin\/admin-ajax.php"};
    /* ]]> */
    </script>


    So I'm interested in how MODX passes PHP variables to Javascript.
      • 22303 MODX Staff
      • 10,725 Posts
      Quote from: Everettg_99 at Dec 27, 2011, 01:41 PM

      So I'm interested in how MODX passes PHP variables to Javascript.
      However you want it to. MODX does not interfere with the output you want to generate, nor should it IMO.
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        Can you provide an example of how to pass PHP variables to Javascript? E.g. localized language strings or a URL like the OP was talking about?

        It's pretty easy to do this via template variables, but are there some examples of AJAX requests in the docs somewhere? There's a jQuery example here: http://rtfm.modx.com/display/revolution20/Loading+Pages+in+the+Front-End+via+AJAX+and+jQuery+Tabs

        But how about JS Ext stuff in the manager?
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: Everettg_99 at Dec 27, 2011, 02:28 PM
          But how about JS Ext stuff in the manager?
          That's a whole other topic, and a bit off-topic here discussing friendly_url settings and generating MODX URLs for use in AJAX requests in the front-end Contexts. Let's take that to another thread.
            • 4172
            • 5,888 Posts
            @Everettg_99
            this is untested code, but something like that should do it:

            [[makeScriptVar? &varname=`my_var` &stuff=`value`&ajax_url=`[[~999]]`]]
            


            <?php
            //[[makeScriptVar? &varname=`my_var` &stuff=`value`&ajax_url=`[[~999]]`]]
            
            $properties['varname']=$scriptProperties['varname'];
            unset($scriptProperties['varname']);
            $properties['data'] = "'".$modx->toJson($scriptProperties)."'";
            
            $tpl = '
            <script type="text/javascript">
            /* <![CDATA[ */
            var [[+varname]] = [[+data]];
            /* ]]> */
            </script>
            ';
            
            $chunk = $modx->newObject('modChunk');
            $chunk->setCacheable(false);
            $chunk->setContent($tpl);
            $html = $chunk->process($properties);
            
            $modx->regClientStartupHTMLBlock($html);
            ?>
            


              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!