Home > PHP > A quick howto of Ajax with PHP

A quick howto of Ajax with PHP

I have been using Ajax for one of our main applications with PHP. We are using the Xajax PHP class libary for that purpose which gives a very easy to use interface.  I thought of sharing a very simple howto for the xajax library in case anyone wants to have a quick look at it…

//Include the xajax class library
require_once(“xajax_core/xajax.inc.php”);

//Initialize a new Xajax object
$xajax = new xajax();

//Register the name of function that you will be calling from Xajax
$xajax->registerFunction(“myFunction”);

//Now the function that uses xajaxResponse object for returning XML
function test_func($arg)
{
$display_val = “Passed to function: “.$arg;
$objResponse = new xajaxResponse();

// add a command to the response to assign the innerHTML attribute of
// the element with id=”v_ElementId” to whatever the new content is
$objResponse->assign(“v_ElementId”,”innerHTML”, $display_val);

//return the  xajaxResponse object
return $objResponse;
}

//Before your script sends any output, use the xajax handle for any requests
$xajax->processRequest();

//Tells ajax to generate the appropriate Javascript
<?php $xajax->printJavascript(); ?>

//Now you can use the function in your HTML code like this
<div id=”v_ElementId”></div>
<button onclick=”xajax_myFunction(‘Hello World!”);”>


Shoaib Mir
shoaibmir[@]gmail.com

Advertisements
Categories: PHP Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: