Error: Could not fetch http://thedevnet.com/wp-content/plugins/gd-star-rating/css/gdsr.css.php?t=0&s=a10i10m20k20c05r05%23121620243046%23121620243240%23s1pchristmas%23s1pcrystal%23s1pdarkness%23s1poxygen%23s1goxygen_gif%23s1pplain%23s1ppumpkin%23s1psoft%23s1pstarrating%23s1pstarscape%23t1pclassical%23t1pstarrating%23t1gstarrating_gif&ver=1.7.2 for caching. You might need to exclude this file in WP Minify options. The Developer Network – Putting AJAX Into Your PHP

Putting AJAX Into Your PHP

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):

  1. <?
  2.  // Xajax – Making PHP and AJAX Communicate Easily
  3.  // http://thedevnet.com/php/classes/putting-ajax-into-your-php/
  4.  require_once("xajax_core/xajax.inc.php");
  5.  
  6.  // call the object
  7.  // (if no script given then current one will be used; remember to call $xajax->processRequest(); before output!)
  8.  // put this in the front end (what the user interacts with)
  9.  $xajax = new xajax("backend.php");
  10.  
  11.  // *** add your functions below here (from backend) ***:
  12.  // $xajax->registerFunction("myFunction");
  13.  
  14.  // show some love..
  15.  echo '<?xml version="1.0" encoding="UTF-8"?>'
  16. ?>

Step 2 [frontend] (insert between your head tag):

  1. <?php $xajax->printJavascript(); ?>

Step 3 [backend] (add at end of script):

  1. <?
  2.  // turn on output buffering
  3.  ob_start();
  4.  
  5.  // include the class
  6.  require_once ("xajax_core/xajax.inc.php");
  7.  
  8.  // put your functions after here:
  9.  
  10.  // this will output any pending data to be sent:
  11.  $xajax->processRequest();
  12. ?>

Example (frontend.php):

  1. <?
  2.  # script name: frontend.php
  3.  // Xajax – Making PHP and AJAX Communicate Easily
  4.  require_once("xajax_core/xajax.inc.php");
  5.  // call the object
  6.  // (if no script given then current one will be used; remember to call $xajax->processRequest(); before output!)
  7.  $xajax = new xajax("backend.php");
  8.  
  9.  // register functions
  10.  $xajax->registerFunction("myFunction");
  11.  
  12.  echo '<?xml version="1.0" encoding="UTF-8"?>'
  13. ?>
  14. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  15. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  16. <head>
  17.  <title>AJAX This!</title>
  18.  <?php $xajax->printJavascript(); ?>
  19. </head>
  20.  
  21. <body>
  22.  <div id="SomeElementId"></div>
  23.  <button onclick="xajax_myFunction('It worked!');" />
  24. </body>
  25. </html>

Example (backend.php):

  1. <?
  2.  # script name: backend.php
  3.  ob_start();
  4.  require_once("xajax_core/xajax.inc.php");
  5.  
  6.  function myFunction($arg)
  7.  {
  8.   $newContent = "Value of $arg: ".$arg;
  9.  
  10.   // Instantiate the xajaxResponse object
  11.   $objResponse = new xajaxResponse();
  12.  
  13.   // add a command to the response to assign the innerHTML attribute of
  14.   // the element with id="SomeElementId" to whatever the new content is
  15.   $objResponse->assign("SomeElementId","innerHTML", $newContent);
  16.  
  17.   // other ways to use the response object:
  18.   /*
  19.   $objResponse->assign("myInput1","value",$DataFromDatabase);
  20.   $objResponse->assign("myInput1","style.color","red");
  21.   $objResponse->append("myDiv1","innerHTML",$DataFromDatabase2);
  22.   $objResponse->prepend("myDiv2","innerHTML",$DataFromDatabase3);
  23.   $objResponse->replace("myDiv3","innerHTML","xajax","<strong>xajax</strong>");
  24.   $objResponse->script("var x = prompt("Enter Your Name");");
  25.   */
  26.  
  27.   //return the  xajaxResponse object
  28.   return $objResponse;
  29.  }
  30.  
  31.  $xajax->processRequest();
  32.  
  33. ?>
xAjax Standard
xAjax_0.5_standard.zip
Version: 0.5

235.7 KiB
27 Downloads
Details…

GD Star Rating
loading...

Originally posted 2009-10-21 01:51:18.

Popularity: 26%

Posted by natster   @   2 August 2010
Tags : , , ,

Related Posts

Like this post? Share it!

RSS Digg Twitter StumbleUpon Delicious Technorati Facebook

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

You must be logged in to post a comment.

Previous Post
« Easily Zip Your Files Using PHP
Next Post
PHP Simple Script Security »
Powered by Wordpress   |   Lunated designed by ZenVerse