Location: PHPKode > projects > PRAjax - PHP Reflected Ajax > examples/example_addressbook.php.txt
<?php
// Set the header
header("Content-Type: text/html; charset=utf-8");

// Use bin or src?
$strSources = '../bin';

// Import PRAjax
require_once($strSources . '/PRAjax.php');

// Classes
class Person {
    var $name;
		var $location;
    
    function Person($name = '', $location = '') {
        $this->name = $name;
        $this->location = $location;
    }
}

class AddressBook {
	var $iCounter = 0;
    var $aAddresses;
    
    function AddressBook() {
        $this->aAddresses = array();
    }
		
		function add($oPerson) {
				$this->aAddresses["" . $this->iCounter++] = $oPerson;
		}	
		
		function delete($iNum) {
				unset($this->aAddresses[$iNum]);
		}	
		
		function get($iNum) {
				return $this->aAddresses[$iNum];
		}		

    function clear() {
        $this->aAddresses = array();
    }
}

// Functions
function GetAddressBook() {
	global $_SESSION;

	if ($_SESSION['makeslowcalls']) sleep(3);
	
  return $_SESSION['addressbook'];
}

function GetPerson($iNum) {
	global $_SESSION;

	if ($_SESSION['makeslowcalls']) sleep(3);
	
	return $_SESSION['addressbook']->get($iNum);
}

function AddPerson($oPerson = NULL) {
	global $_SESSION;
	
	if ($_SESSION['makeslowcalls']) sleep(3);
	
	$_SESSION['addressbook']->add($oPerson);
}

function SetMakeSlowCalls() {
	global $_SESSION;

	if (!isset($_SESSION['makeslowcalls'])) {
		$_SESSION['makeslowcalls'] = false;
	}
	
	$_SESSION['makeslowcalls'] = !$_SESSION['makeslowcalls'];
}

// Session
session_start();

// Initializiation
if (!isset($_SESSION['addressbook'])) {
	$_SESSION['addressbook'] = new AddressBook();
	$_SESSION['addressbook']->add(new Person("Maarten Balliauw", "Belgium"));
	$_SESSION['addressbook']->add(new Person("John Doe", "USA"));
	$_SESSION['addressbook']->add(new Person("Peter Johnsson", "United Kingdom"));
	$_SESSION['addressbook']->add(new Person("Hans Beck", "Germany"));
	$_SESSION['addressbook']->add(new Person("Dieter Muller", "Germany"));
}

// Instantiate PRAjax
$objPRAjax = new PRAjax($strSources . '/');

// Register functions
$objPRAjax->RegisterFunction('GetAddressBook');
$objPRAjax->RegisterFunction('GetPerson');
$objPRAjax->RegisterFunction('AddPerson');
$objPRAjax->RegisterFunction('DeletePerson');
$objPRAjax->RegisterFunction('SetMakeSlowCalls');
$objPRAjax->RegisterClass('Person');
$objPRAjax->RegisterClass('AddressBook');
$objPRAjax->ShowBusyMessage(true, '<img src=\'timer.gif\' align=\'absmiddle\' hspace=\'4\'> Busy...', 'prajax_status');
$objPRAjax->ShowWaitCursor();

// Handle PRAjax client request
$objPRAjax->HandleClientRequest();
?>
<html>
<head>
<title>Address book</title>
<?php $objPRAjax->GetJavaScript(); ?>
<script language="JavaScript">
	<!--

	// Address book
	var oAddressBook = new addressbook();
	
	// Callback functions
	function GetAddressBook_cb(pAddressBook) {
		if (oAddressBook != null) {
		    oAddressBook = pAddressBook;
			UpdateUI();
		}
	}
	
	function GetPerson_cb(oPerson) {
		if (oPerson != null) {
			document.getElementById("spnName").innerHTML = oPerson.name;
			document.getElementById("spnLocation").innerHTML = oPerson.location;
		}
	}
	
	function NoAction_cb(data) {
		GetAddressBook(GetAddressBook_cb);
	}
	
	// Client functions
	function NewPerson(name, location) {
		var oPerson = new person(name, location);
		AddPerson(oPerson, NoAction_cb);
	}
	
	function UpdateUI() {
		var lstPersons = document.getElementById('lstPersons');
		
		// Clean list
		lstPersons.selectedIndex = -1;
		lstPersons.options.length = 0;
		
		// Add options
		for (var i = 0; i < oAddressBook.aAddresses.length; i++) {
			lstPersons.options[i] = new Option(oAddressBook.aAddresses[i].name, i);
		}
	}
	// -->
    </script>
<style type="text/css">
<!--
body, p, td {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 11px;
}
h1 {
	font-size: 22px;
	font-weight: bold;
}
h2 {
	font-size: 18px;
	font-weight: bold;
}
-->
</style>
</head>
<body onLoad="GetAddressBook(GetAddressBook_cb);">
<div style="width: 100%; height: 65px; background-color: #EEEEEE; font-family: Arial, Helvetica, Sans-Serif; font-size: 9pt;">
This example demonstrates the use of PHP objects within JavaScript code.
<br>
<a href="example_addressbook.php.txt" target="_blank">View code...</a>
</div>
<form name="frmForm">
<h1>Address book</h1>
<table width="400"  border="0" cellspacing="0" cellpadding="2">
  <tr>
    <td bgcolor="#CCCCCC"><h2>Persons</h2></td>
    <td bgcolor="#CCCCCC"><h2>Details</h2></td>
    <td bgcolor="#CCCCCC">&nbsp;</td>
  </tr>
  <tr>
    <td rowspan="7" valign="top"><select name="lstPersons" size="12" id="lstPersons" onChange="if (this.selectedIndex >= 0) { GetPerson('' + this.selectedIndex, GetPerson_cb);}"></select></td>
    <td>Name:</td>
    <td><span id="spnName"></span></td>
  </tr>
  <tr>
    <td>Location:</td>
    <td><span id="spnLocation"></span></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td bgcolor="#CCCCCC"><h2>New person </h2></td>
    <td bgcolor="#CCCCCC">&nbsp;</td>
  </tr>
  <tr>
    <td>Name:</td>
    <td><input name="txtName" type="text" id="txtName" size="10"></td>
  </tr>
  <tr>
    <td>Location:</td>
    <td><input name="txtLocation" type="text" id="txtLocation" size="10"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input name="btnAdd" type="button" id="btnAdd" value="Add" onClick="NewPerson(this.form.txtName.value, this.form.txtLocation.value); this.form.txtName.value = ''; this.form.txtLocation.value = '';"></td>
  </tr>
  <tr>
    <td colspan="3"><input name="chkMakeSlowCalls" type="checkbox" id="chkMakeSlowCalls" value="true" onClick="SetMakeSlowCalls('' + this.checked);"> 
      Make slow calls (server functions 'sleep' for 3 seconds) </td>
  </tr>
  <tr>
    <td colspan="3"><span id="prajax_status"></span></td>
    </tr>
</table>
</form>
</body>
</html>
Return current item: PRAjax - PHP Reflected Ajax