• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

AJAX tricks enpowered by XAJAX libs.

tarjei

Necronian Engineer
Joined
May 25, 2008
Messages
505
Reaction score
126
Location
Poland
@edit Mod, please move it to website resources section, I posted it here by accident.

Hello, I want to introduce you really fabulous library that I've found nowadays.
What is it, that powerful in it? It allows you to make asynchronous calls to server using javascript, basicly without reloading your page.
The truth is, that you can make all of this without this library, but it simplifies everything related to asynchronous calls.
Let's get on a ride then, shall we? :D

In the first place, we need to make some preparations - include library php file that we downloaded, and make our async php function.

PHP:
<?php
       require_once( "../xajax_core/xajax.inc.php" );
    function asyncCall($aInputData) 
    { 


        $objResponse=new xajaxResponse(); 
        $link = mysql_connect('localhost:3306', 'root', ''); 
        mysql_select_db('foo', $link); 
     

        mysql_close($link); 
       //this will popup alert window with values from a form passed onclick :D
        $objResponse->alert( "inputData: " . print_r( $aInputData, true )); 

        return $objResponse; 
    }
Maybe some pause in here.
$objResponse is holds data (actions and stuff) that will be send back to browser. You can make it alert stuff (like a javascript function) but it has to be returned
as function value. Moreover you can modify your DOM tree node using $objResponse from php.
In example above I have just connected with database, but you can extract what you need, and make proper changes to the site. All of this is possible without reloading!

Below:
We also need to add some essential lines between php tag.
PHP:
$xajax=new xajax();
//It is handy to have debugger on, you can always comment this line.
$xajax->configure("debug", true);
//This one registers our php function and makes it accesible from java script, with name xajax_asyncCall
$xajax->register(XAJAX_FUNCTION, "asyncCall");



$xajax->processRequest();

//Make sure that scripts are in correct location.
$xajax->configure('javascript URI','../');
$xajax->configure('responseType','XML');
?>

Let's give it some spice now :D




PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<body>
    <head>
        <title>Welcome in Necronia translation panel.</title>

        <?php $xajax->printJavascript( "../" ) ?>
    </head>
      
            <form id = "languageForm" onsubmit = "return false;">
                   <div>
                         Select language you will translate to:
                    </div>
                    <select id = "inputLanguage" name = "inputLanguage">
                             <option value = "polish" >polish</option>




                             <option value = "spanish">spanish</option>




                             <option value = "swedish">swedish</option>




                             <option value = "bosnian">bosnian</option>




                             <option value = "german">german</option>
                       </select>		
                       <input type = "submit" value = "Obtain texts from database"
                        onclick = "xajax_asyncCall(xajax.getFormValues('languageForm')); return false;" />				
                   </form>
          </body>
</html>
Stuff between <head> is required in order to make functions work properly (binded ones). argument of printJavascript is relative directory to core java scripts from a lib. I guess even in case of errors you can easily handle it by your own.


So this is a sample form we can make. Notice that submit button is connected with some function and it's going to be triggered onclick.
xajax.getFormValues pass array to our php function with all values in form with name languageForm.

You can find more and more examples on provided link in a package. This library is a really nice tool that enpower AJAX. It is really worth of trying it out. :D

Download :
Downloads| xajax PHP Class Library- The easiest way to develop asynchronous Ajax applications with PHP
 
Last edited:
Back
Top