Log in

View Full Version : [PHP] AJAXReplacer Class



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.

ZAG
15-02-2007, 06:36 PM
This is a bit pointless than the other one but I know you haven't made many classes before.

Good though.

nets
15-02-2007, 07:05 PM
the class would cause yuor html to become repetitive. preferably, an external javascript file would contain a one-off function to perform such tasks (preventing inefficient homogeneous code).

(-:

timROGERS
15-02-2007, 07:17 PM
This is a bit pointless than the other one but I know you haven't made many classes before.

Good though.

Yeah, this one was just another idea that I had - it's not really particuarly useful, I agree.


the class would cause yuor html to become repetitive. preferably, an external javascript file would contain a one-off function to perform such tasks (preventing inefficient homogeneous code).

(-:

I did think of that. It writes really horrible code, which I dislike to. It's difficult to read because it doesn't use line breaks, it just puts the coding into one long line.

Mentor
15-02-2007, 09:18 PM
Kudos on the OOP approach, although i dont really see then benifit over simply writeing in the javascript function by hand, and supplying the relivant varibles to it, avoideing the needless php o.0

Want to hide these adverts? Register an account for free!