Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP ... and his friend AJAX.
#1
The goal of this tutorial is to learn you a bit about Ajax langage, and make quickly something working.
First of all, I'm going to quote the definition about Ajax in Wikipedia.

Definition :
AJAX (shorthand for asynchronous JavaScript and XML) is a group of interrelated web development techniques used on the client-side to create interactive web applications. With AJAX, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page.


In other term : You can retrieve data from server, without reloading your page.

Let's see some code :
Here is my Ajax function, coded in Javascript :

Quote:<script type="text/javascript">
function ajax_request(data,div,file) {
var XHR = null;
if (window.XMLHttpRequest) // Firefox, Safari, Opera, most of browsers.
XHR = new XMLHttpRequest();
else if (window.ActiveXObject) // Internet Explorer
XHR = new ActiveXObject("Microsoft.XMLHTTP");
else { // The browser does not support XMLHttpRequest
alert("Error : Your browser does not support XMLHttpRequest.");
return false;
}
XHR.onreadystatechange = function waiting() {
if (XHR.readyState == 4){
document.getElementById(div).innerHTML = XHR.responseText;
}
}
XHR.open("POST",file, true);
XHR.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
XHR.send(data);
return false;
}
</script>

The code is not that hard to understand : We create an HttpRequest or ActiveXObject depending of the browser, we send the data via POST, and we get the new informations and write it on the page.

Well, you don't really need to know how everything works in this function, let's see now the parameters :

function ajax_request(data,div,file)
  • data : It is the post data which we are going to send to the php script. You have to write it like that : "var1=v1&var2=v2&var3=v3...etc"
  • div : It is the container where the response will be written
  • file : It is the php file which will be requested for the process.


You want an example ?

ajaxfunction.js :
(you already know it !)
Quote:function ajax_request(data,div,file) {
var XHR = null;
if (window.XMLHttpRequest) // Firefox, Safari, Opera, most of browsers.
XHR = new XMLHttpRequest();
else if (window.ActiveXObject) // Internet Explorer
XHR = new ActiveXObject("Microsoft.XMLHTTP");
else { // The browser does not support XMLHttpRequest
alert("Error : Your browser does not support XMLHttpRequest.");
return false;
}
XHR.onreadystatechange = function waiting() {
if (XHR.readyState == 4){
document.getElementById(div).innerHTML = XHR.responseText;
}
}
XHR.open("POST",file, true);
XHR.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
XHR.send(data);
return false;
}

Index.html : (a form which asking your name ...)
Code:
<html>
<head>
<title>Ajax -- Support Forums Tutorial</title>
<script type="text/javascript" src="ajaxfunction.js"></script>
</head>

<body>
<form name="form" onSubmit="ajax_request('promptbox='+document.getElementById('promptbox').value,'answer','phpanswer.php');return(false);">
    Enter your name > <input type="text" id="promptbox">
</form><br />

<div id="answer">
Php is sleeping.
</div>

</body>
</html>

phpanswer.php (php will receive data and will answer !)
PHP Code:
<?php
if (isset($_POST['promptbox'])){
    echo 
"PHP says : Hello ".$_POST['promptbox']." ! *yawns*";
}
?>

So, let's take a look into the code :
onSubmit="ajax_request('promptbox='+document.getElementById('promptbox').value,'answer','phpanswer.php')"
  1. The first parameter send "promptbox="+the value into the promptbox
  2. It will send the data into the div id = "answer"
  3. And it will request the phpanswer.php file !
Want to see what it does ?
You can take quiclky a look on this link, i've copied / paste the code :
http://supportforum.alwaysdata.net/Ajax-demo/

Too hard to copy/paste it all ?
You can download all the sources here :
http://supportforum.alwaysdata.net/Ajax-...x-demo.zip

Thanks for reading this tutorial Smile
Hope it helps!


PS : I didn't really know where to post it, there wasn't any Javascript section ... so i've posted it into PHP ...
Reply


Messages In This Thread
PHP ... and his friend AJAX. - by Spl3en - 10-14-2009, 10:05 AM
RE: PHP ... and his friend AJAX. - by Combo - 10-15-2009, 09:09 AM
RE: PHP ... and his friend AJAX. - by Spl3en - 10-15-2009, 11:47 AM
RE: PHP ... and his friend AJAX. - by h1r0n - 10-16-2009, 03:42 AM
RE: PHP ... and his friend AJAX. - by Spl3en - 10-16-2009, 08:18 AM
RE: PHP ... and his friend AJAX. - by immi - 10-16-2009, 09:04 AM
RE: PHP ... and his friend AJAX. - by Gaijin - 01-10-2010, 08:36 AM
RE: PHP ... and his friend AJAX. - by Spl3en - 01-10-2010, 09:19 PM
RE: PHP ... and his friend AJAX. - by uber1337 - 01-11-2010, 06:59 PM
RE: PHP ... and his friend AJAX. - by Gaijin - 01-11-2010, 11:34 PM
RE: PHP ... and his friend AJAX. - by Spl3en - 01-12-2010, 11:47 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 941 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,668 10-10-2011, 01:00 PM
Last Post: Greyersting
  [JS] AJAX Comment System Project Evolution 13 6,017 05-14-2011, 02:18 AM
Last Post: モrainee
  [Release]PHP/AJAX Commenting Script Saint Michael 12 5,535 12-23-2010, 10:30 AM
Last Post: Buhrock
  [JS/jQuery] Implementing AJAX into your website Project Evolution 0 908 08-05-2010, 08:29 PM
Last Post: Project Evolution

Forum Jump:


Users browsing this thread: 2 Guest(s)