This script is an extremely simple approach to communicating with your PHP using AJAX. Dynamically updating your pages without needing to reload your pages has never been this easy. Clean up your interface with a little bit of AJAX.
Step 1 [frontend] (include the class and initiate the xajax object):
-
<?
-
// Xajax – Making PHP and AJAX Communicate Easily
-
// http://thedevnet.com/php/classes/putting-ajax-into-your-php/
-
require_once("xajax_core/xajax.inc.php");
-
-
// call the object
-
// (if no script given then current one will be used; remember to call $xajax->processRequest(); before output!)
-
// put this in the front end (what the user interacts with)
-
$xajax = new xajax("backend.php");
-
-
// *** add your functions below here (from backend) ***:
-
// $xajax->registerFunction("myFunction");
-
-
// show some love..
-
echo '<?xml version="1.0" encoding="UTF-8"?>'
-
?>
Step 2 [frontend] (insert between your head tag):
-
<?php $xajax->printJavascript(); ?>
Step 3 [backend] (add at end of script):
-
<?
-
// turn on output buffering
-
ob_start();
-
-
// include the class
-
require_once ("xajax_core/xajax.inc.php");
-
-
// put your functions after here:
-
-
// this will output any pending data to be sent:
-
$xajax->processRequest();
-
?>
Example (frontend.php):
-
<?
-
# script name: frontend.php
-
// Xajax – Making PHP and AJAX Communicate Easily
-
require_once("xajax_core/xajax.inc.php");
-
// call the object
-
// (if no script given then current one will be used; remember to call $xajax->processRequest(); before output!)
-
$xajax = new xajax("backend.php");
-
-
// register functions
-
$xajax->registerFunction("myFunction");
-
-
echo '<?xml version="1.0" encoding="UTF-8"?>'
-
?>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-
<head>
-
<title>AJAX This!</title>
-
<?php $xajax->printJavascript(); ?>
-
</head>
-
-
<body>
-
<div id="SomeElementId"></div>
-
<button onclick="xajax_myFunction('It worked!');" />
-
</body>
-
</html>
Example (backend.php):
-
<?
-
# script name: backend.php
-
ob_start();
-
require_once("xajax_core/xajax.inc.php");
-
-
function myFunction($arg)
-
{
-
$newContent = "Value of $arg: ".$arg;
-
-
// Instantiate the xajaxResponse object
-
$objResponse = new xajaxResponse();
-
-
// add a command to the response to assign the innerHTML attribute of
-
// the element with id="SomeElementId" to whatever the new content is
-
$objResponse->assign("SomeElementId","innerHTML", $newContent);
-
-
// other ways to use the response object:
-
/*
-
$objResponse->assign("myInput1","value",$DataFromDatabase);
-
$objResponse->assign("myInput1","style.color","red");
-
$objResponse->append("myDiv1","innerHTML",$DataFromDatabase2);
-
$objResponse->prepend("myDiv2","innerHTML",$DataFromDatabase3);
-
$objResponse->replace("myDiv3","innerHTML","xajax","<strong>xajax</strong>");
-
$objResponse->script("var x = prompt("Enter Your Name");");
-
*/
-
-
//return the xajaxResponse object
-
return $objResponse;
-
}
-
-
$xajax->processRequest();
-
-
?>
| Author: |
xajaxproject.org |
| Platforms: |
Web Based |
| Date: |
October 21, 2009 |
GD Star Rating
loading...
Originally posted 2009-10-21 01:51:18.
Popularity: 26%
Posted by natster @ 2 August 2010