The fastest easiest way to get it right.

Returning Script with JSON

It may be that you wish to have some bit of javascript execute when your JSON call is returned to the browser. You can do this with the x6script() function, which takes as its argument a fragment of Javascript that will execute on the browser.

The example below is identical to the prior page, except for the very last line, which adds script to the JSON return payload.

<?php
class x6jsonexample extends androX6 {
    function x6main() {
        # Conventional HTML generation...
        $top = html('div');
        $top->h('h1','Hello JSON!');
        $top->h('p' ,'The server time is:');
        
        # Give this element an ID, so it can be
        # refreshed.
        $p = $top->h('p' ,date('r',time()));
        $p->hp['id'] = 'the_time_paragraph';
        
        $input = $top->h('input','Refresh Time');
        $input->hp['type'] = 'button';
        $input->code['click'] = <<<JS
        function(e) {
            x6.json.init('x6page','jsonexample');
            x6.json.addParm('x6action','refreshtime');
            if(x6.json.execute()) {
                x6.json.process();
            }
        }
JS;
        $top->render();
    }
    
    # This routine handles the call
    function refreshtime() {
        x6html('the_time_paragraph',date('r',time()));
        x6script("alert('Now you know what time I think it is')");
    }
}
?>

You can have any amount of script returned with the x6script, the only practical limit is bandwidth and the browser's processing power.

It is safe to put SCRIPT tags around the script, the x6script() routine strips them out, so the following is perfectly safe:

<?php
class x6jsonexample extends androX6 {
    function x6main() {
        # .... 
    }
    
    function refreshtime() {
        ob_start();
        ?>
        
        <?php
        x4Script(ob_get_clean());
    }
}
?>

User Comments

There are no user comments yet on this page.


Add A Comment

Comments will not appear until after they are moderated. Comments are usually moderated within a few hours on weekdays, but may take longer on weekends and holidays.

Name or nickname: (This will appear with your comment)


Email (this will never be displayed)


Enter your comment here. Use [b] and [/b] for bold, [i] and [/i] for italic, and [pre] and [/pre] for code samples. All literal HTML and PHP that you enter will be escaped out and displayed as you enter it.


Home |  Documentation |  Download |  Credits |  Contact |  Login
Andromeda © Copyright 2004-2010, Licensed under the GPL Version 2