Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    3,285
    Tokens
    0

    Latest Awards:

    Default [PHP] AJAXReplacer Class

    Yes, I know, it's not really AJAX, it's just Javascript, it's only AJAX if you dynamically get the data 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 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 Code:
    <?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 Code:
    <?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 .
    Last edited by timROGERS; 15-02-2007 at 06:14 PM. Reason: Added sample.



    i used to be NintendoNews. visit my blog or add me on twitter.
    need help with vista? i am a microsoft certified technology specialist in configuring windows vista and connected home integrator.. pm me for help!


    "I am the way, the truth, and the life. No one comes to the Father except through me"
    John 14:6 (NIV)


  2. #2
    Join Date
    Jan 2007
    Location
    West Yorkshire
    Posts
    384
    Tokens
    0

    Default

    This is a bit pointless than the other one but I know you haven't made many classes before.

    Good though.

    “two players, two sides.
    one is light, one is dark.”
    - John Locke

  3. #3
    Join Date
    Aug 2004
    Location
    bristol
    Posts
    3,799
    Tokens
    0

    Latest Awards:

    Default

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

    (-:
    kinda quit.

  4. #4
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    3,285
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by Th0m4s View Post
    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.

    Quote Originally Posted by nets View Post
    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.



    i used to be NintendoNews. visit my blog or add me on twitter.
    need help with vista? i am a microsoft certified technology specialist in configuring windows vista and connected home integrator.. pm me for help!


    "I am the way, the truth, and the life. No one comes to the Father except through me"
    John 14:6 (NIV)


  5. #5
    Join Date
    Aug 2004
    Location
    UK
    Posts
    11,283
    Tokens
    2,031

    Latest Awards:

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •