Subscribe to The New and Improved Ruby Codes        RSS Feed

03_call php file from html using the prototype lib

Icon Leave Comment
Ok, in our last example, we called a PHP file from within a web page. It was a fairly simple procedure to understand, but it took a lot of lines. Like 30 or 40 I think! That's crazy, especially considering most of it was browser compatibility workarounds. So instead of using AJAX that way, we're better off using the prototype library.

prototype.js can be obtained from:

You put it in the same directory as your .html file and add this line:

        <script src="prototype.js" type="text/javascript"></script>

If you're using netbeans, it will automatically look through the .js file and offer you code completion assistance!

OK, so let's set you up with the examples:


<!DOCTYPE html>
        <script src="prototype.js" type="text/javascript"></script>
            function bla()
                var url = "getme.php";
                var pars = "foo=bar";  //  so it will call like this:
                var myBlaObj = $('printhere');
                var myAjax = new Ajax.Request(
                                    method: 'get', 
                                    parameters: pars,
                                    onComplete: showResponse
            function showResponse(originalRequest)
                //put returned XML in the textarea
                var elementToPrintTo = $('printhere');
                elementToPrintTo.innerHTML = originalRequest.responseText;
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Call a PHP using prototype's AJAX support</title>
            // put your code here
            echo "this is a test php page <br>";
        <input type="button" value="Clicky!" onclick="bla();"/>
        <div id="printhere"> </div>


    echo "YOU GOT ME!  =)";

That code should work out for you and when you click the button, you will see "YOU GOT ME! =)" on the html page.

0 Comments On This Entry


Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

October 2015

4 5678910


    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)