The big difference is in the amount of javascript your writing and the feel of your application
Don't overuse ajax, the usability requirements for javascript applications are quite different than the requirements for regular web pages
These are mainly taken from an article by Thomas Baekdal
// Include the needed classes
require_once 'JPSpan/JPSpan.php'; require_once JPSPAN . 'Server/PostOffice.php';
// Create the PostOffice server
$S = & new JPSpan_Server_PostOffice();
// Register your class with it...
$S->addHandler(new username_validator());
if (isset($_SERVER['QUERY_STRING'])
&& strcasecmp($_SERVER['QUERY_STRING'], 'client')==0) {
$S->displayClient(); // Display the Javascript client
} else {
require_once JPSPAN . 'ErrorHandler.php'; // PHP errors serialized to JS
// Start serving requests...
$S->serve();
}
I'm only showing the method that does actual work
function check_username($username) {
$ret = new stdClass();
$ret->status = true;
if (in_array($username,$_SESSION['usernames'])) {
$ret->status = false;
// make the current username unique
$i = 1; $new = $username.$i++;
while(in_array($new,$_SESSION['usernames'])) {
$new = $username.$i++;
}
$ret->message = "Username $username already in use,".
" maybe you could use '$new' instead.";
}
return $ret;
}
The javascript proxy object is autogenerated from the php class in Server.php you could easily add a cache to increase scalability
<script type='text/javascript' src='server.php?client'></script>
<script>
// our remote proxy object
var usernameValidator = new username_validator(new valCallback());
// our callback class
function valCallback() {
}
valCallback.prototype.check_username = function(result) {
if (!result.status) {
setFormMessage(result.message);
...
}
}
function validateUsername(inSubmit) {
var username = document.getElementById('username').value;
if (inSubmit && usernameValidator.__client.xmlhttp.callInProgress()) {
usernameValidator.__client.xmlhttp.abort();
usernameValidator.Sync();
var result = usernameValidator.check_username(username);
if (!result.status) {
setMesage(result.message);
return false;
}
}
else {
usernameValidator.check_username(username);
}
}
To log JPSpan errors and success you need to write an observer class and setup the server to load it
class observer() {
function error($data) {
$fp = fopen('error.log','a');
fwrite($fp,var_export($data,true));
fclose($fp);
}
}
// setup for logging
define ('JPSPAN_MONITOR', TRUE);
require_once JPSPAN . 'Monitor.php';
$monitor = & JPSpan_Monitor::instance();
$monitor->addObserver(new observer());
For a group of people who forget about hygiene for weeks on end developers sure have an obsession with cleanliness. First soap and now ajax, all we need is dawn and palmolive and you'll always have something to wash your hands and dishes.
If your reading this online feel free to leave questions/comments on my blog