PHP Classes

File: hello.php

Recommend this page to a friend!
  Classes of Thierry Feuzeu   Jaxon   hello.php   Download  
File: hello.php
Role: Example script
Content type: text/plain
Description: Example of Jaxon function
Class: Jaxon
Call PHP classes from JavaScript using AJAX
Author: By
Last change:
Date: 7 years ago
Size: 2,397 bytes
 

Contents

Class file image Download
<?php

require (__DIR__ . '/vendor/autoload.php');

use
Jaxon\Jaxon;
use
Jaxon\Response\Response;
use
Jaxon\Request\Factory as xr;

$jaxon = jaxon();

/*
    Function: helloWorld
   
    Modify the innerHTML of div1.
*/
function helloWorld($isCaps)
{
    if (
$isCaps)
       
$text = 'HELLO WORLD!';
    else
       
$text = 'Hello World!';
       
   
$xResponse = new Response();
   
$xResponse->assign('div1', 'innerHTML', $text);
   
    return
$xResponse;
}

/*
    Function: setColor
   
    Modify the style.color of div1
*/
function setColor($sColor)
{
   
$xResponse = new Response();
   
$xResponse->assign('div1', 'style.color', $sColor);
   
    return
$xResponse;
}

// Register functions
$jaxon->register(Jaxon::USER_FUNCTION, 'helloWorld');
$jaxon->register(Jaxon::USER_FUNCTION, 'setColor');

// Process the request, if any.
$jaxon->processRequest();

?>
<script type='text/javascript'>
    /* <![CDATA[ */
    window.onload = function() {
        // call the helloWorld function to populate the div on load
        <?php echo xr::call('helloWorld', 0) ?>;
        // call the setColor function on load
        <?php echo xr::call('setColor', xr::select('colorselect')) ?>;
    }
    /* ]]> */
</script>
                        <div class="col-md-12" id="div1">
                            &nbsp;
                        </div>
                        <div class="col-md-4 margin-vert-10">
                            <select class="form-control" id="colorselect" name="colorselect"
                                    onchange="<?php echo xr::call('setColor', xr::select('colorselect')) ?>; return false;">
                                <option value="black" selected="selected">Black</option>
                                <option value="red">Red</option>
                                <option value="green">Green</option>
                                <option value="blue">Blue</option>
                            </select>
                        </div>
                        <div class="col-md-8 margin-vert-10">
                            <button type="button" class="btn btn-primary" onclick="<?php echo xr::call('helloWorld', 0) ?>; return false;" >Click Me</button>
                            <button type="button" class="btn btn-primary" onclick="<?php echo xr::call('helloWorld', 1) ?>; return false;" >CLICK ME</button>
                        </div>