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: http://www.prototypejs.org/download
You put it in the same directory as your .html file and add this line:
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:
(index.html)
(getme.php)
That code should work out for you and when you click the button, you will see "YOU GOT ME! =)" on the html page.
prototype.js can be obtained from: http://www.prototypejs.org/download
You put it in the same directory as your .html file and add this line:
<head>
<script src="prototype.js" type="text/javascript"></script>
...
</head>
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:
(index.html)
<!DOCTYPE html>
<html>
<head>
<script src="prototype.js" type="text/javascript"></script>
<script>
function bla()
{
var url = "getme.php";
var pars = "foo=bar"; // so it will call like this: bla.com/getme.php?foo=bar
var myBlaObj = $('printhere');
var myAjax = new Ajax.Request(
url,
{
method: 'get',
parameters: pars,
onComplete: showResponse
});
}
function showResponse(originalRequest)
{
//put returned XML in the textarea
var elementToPrintTo = $('printhere');
elementToPrintTo.innerHTML = originalRequest.responseText;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Call a PHP using prototype's AJAX support</title>
</head>
<body>
<?php
// put your code here
echo "this is a test php page <br>";
?>
<input type="button" value="Clicky!" onclick="bla();"/>
<div id="printhere"> </div>
</body>
</html>
(getme.php)
<?php
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 ]
Tags
My Blog Links
Recent Entries
-
-
What's a WebAPI anyway? How can I use one? Are https get requests easy in ruby?
on Oct 12 2011 04:38 PM
-
-
-
03_call php file from html using the prototype libon May 18 2011 02:04 PM
Search My Blog
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)
Categories
|
|



Leave Comment









|