timROGERS
15-02-2007, 06:13 PM
Yes, I know, it's not really AJAX, it's just Javascript, it's only AJAX if you dynamically get the data :P But it just sounds a lot better eh? I've written another PHP class, after my RSSReader class, this time which is a bit more simple :P It makes replacing text with Javascript more easy - you can just run the PHP multiple times if you want to have more than one replacing. Here's the code for AJAXReplacer.class.php:
<?php
// AJAXReplacer Class by Tim Rogers
// To use this, place the needed PHP between the <head> tags
// To replace something, call replace_(WHAT YOU SET AS JAVANAME)
class AJAXReplacer {
var $span;
var $javaname;
function replace($replace_with) {
echo '<script type="text/javascript">';
echo 'function replace_';
echo $this->javaname;
echo '() { document.getElementById("';
echo $this->span;
echo '").innerHTML = "';
echo $replace_with;
echo '"; }</script>';
}
}
?>
And here is how to call it, aka test.php:
<?php
// Brings in the class
require("AJAXReplacer.class.php");
?>
<html>
<head>
<title>AJAXReplacer Test</title>
<?php
// AJAXReplacer Test
// Creates a new "AJAXReplacer" object and assigns it to $replacer
$replacer = new AJAXReplacer;
// Sets the span to replace
$replacer->span = "replacehere";
// Sets the name of the function which will be created
$replacer->javaname = "replacetest";
// Creates a function to replace the span with the string in the parameter of replace()
$replacer->replace("This has been replaced");
?>
</head>
<body>
<a onclick="replace_replacetest();"><span id="replacehere">This is going to be replaced</span></a>
</body>
</html>
For a sample of it working, visit http://php5.tim-rogers.co.uk/AJAXReplacer/test.php. It isn't PHP5, but I put it in my PHP5 area as I was already on it :P.
<?php
// AJAXReplacer Class by Tim Rogers
// To use this, place the needed PHP between the <head> tags
// To replace something, call replace_(WHAT YOU SET AS JAVANAME)
class AJAXReplacer {
var $span;
var $javaname;
function replace($replace_with) {
echo '<script type="text/javascript">';
echo 'function replace_';
echo $this->javaname;
echo '() { document.getElementById("';
echo $this->span;
echo '").innerHTML = "';
echo $replace_with;
echo '"; }</script>';
}
}
?>
And here is how to call it, aka test.php:
<?php
// Brings in the class
require("AJAXReplacer.class.php");
?>
<html>
<head>
<title>AJAXReplacer Test</title>
<?php
// AJAXReplacer Test
// Creates a new "AJAXReplacer" object and assigns it to $replacer
$replacer = new AJAXReplacer;
// Sets the span to replace
$replacer->span = "replacehere";
// Sets the name of the function which will be created
$replacer->javaname = "replacetest";
// Creates a function to replace the span with the string in the parameter of replace()
$replacer->replace("This has been replaced");
?>
</head>
<body>
<a onclick="replace_replacetest();"><span id="replacehere">This is going to be replaced</span></a>
</body>
</html>
For a sample of it working, visit http://php5.tim-rogers.co.uk/AJAXReplacer/test.php. It isn't PHP5, but I put it in my PHP5 area as I was already on it :P.