• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

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.?>

 

 Original Source:

AddThis Social Bookmark Button

Posted at 10:38:55 am | Permalink | Posted in PHP  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

e-messenger

MSN Web Messenger

eBuddy

ASP.NET Ajax CalendarExtender and Validation

AIM Express

Ajax Tools for ASP.NET Developers



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (176)
  • Ajax (112)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (84)
  • Email (23)
  • Facebook (84)
  • Flash (20)
  • Google (54)
  • Html (29)
  • Image (12)
  • International Calls & VOIP (7)
  • Java (58)
  • Javascript (280)
  • jQuery (200)
  • JSON (75)
  • Perl (2)
  • PHP (172)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (32)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (227)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (29)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange