Send Data With Ajax and PHP
Send Data With Ajax and PHP
Send Data With Ajax and PHP
text
Description
This tutorial explains how to use Ajax in order to retrieve whenever you want the data
sent by your PHP script in plain text.
Introduction
This tutorial can be realized with a simple text editor like Notepad . This tutorial
requires some basic knowledge in html, JavaScript and PHP.
HTML Form
First of all we need to create a file call form.html. This file will contains only one button
that allows retrieving the data sent by the PHP script.
form.html
<html><head>
<script language="javascript" src="Ajax.js"></script>
</head><body>
<form action="">
<input type="button" onclick="retrieveData()" value="ajax"/>
</form>
</body></html>
As you can see, when we click on the ajax button, the function retrieveData() is
called. This function is defined in the file Ajax_txt.js.
JavaScript script
We will now create a new file called Ajax_txt.js. This file will contains the function that
allows creating an instance that allows sending http requests to the server and another
function that allows retrieving the response from the server.
List of attributes:
List of methods:
For creating XMLHttpRequest object instance, you need to add the following function:
Ajax.js
function createInstance(){
if(window.XMLHttpRequest){
/* Firefox, Opera, Google Chrome */
return new XMLHttpRequest();
}else if(window.ActiveXObject){
/* Internet Explorer */
var names = [
"Msxml2.XMLHTTP.6.0",
"Msxml2.XMLHTTP.3.0",
"Msxml2.XMLHTTP",
"Microsoft.XMLHTTP"
];
for(var i in names){
/* We test the different versions */
try{ return new ActiveXObject(names[i]); }
catch(e){}
}
alert("Not supported");
return null; // Not supported
}
};
PHP script
Now we will write the PHP script on the server side. This script is very simple. Create a
file call server.php and add the following code inside:
server.php
<?php
echo "data";
?>
Tests
This tutorial comes to his end. You can test your application by clicking on ajax
button. The message: data should be displayed.
Description
This tutorial explains how to use ajax in order to send GET or POST data to a PHP script.
Introduction
This tutorial can be realized with a simple text editor like Notepad . This tutorial
requires some basic knowledge in html, JavaScript and PHP.
HTML form
First of all, we will create a file with the name form.html. This file will contains only one
button that allows sending data to the PHP script.
form.html
<html><head>
<script language="javascript" src="Ajax.js"></script>
</head><body>
<form name="myForm" action="">
<input type="text" name="data" value=" "/>
<input type="button" onclick="sendData()" value="send"/>
</form>
</body></html>
We can see that, when we click on the send button, the function sendData() is
called. This function will be defined in the file Ajax_send.js.
JavaScript
We will now create the file called Ajax_send.js. This file will contain the function that
create the instance that allows sending a http request to the server and a function that
send the data and retrieve the server response.
List of methods:
To create the instance of the XMLHttpRequest object, you need to add the following
function:
Ajax.js
function createInstance(){
if(window.XMLHttpRequest){
/* Firefox, Opera, Google Chrome */
return new XMLHttpRequest();
}else if(window.ActiveXObject){
/* Internet Explorer */
var names = [
"Msxml2.XMLHTTP.6.0",
"Msxml2.XMLHTTP.3.0",
"Msxml2.XMLHTTP",
"Microsoft.XMLHTTP"
];
for(var i in names){
/* We test the different versions */
try{ return new ActiveXObject(names[i]); }
catch(e){}
}
alert("Not supported");
return null; // Not supported
}
};
PHP script
We will now write the PHP server-side script. You will see that this script is very simple.
The PHP file that we will create is named: server.php.
server.php
<?php
echo "data recieved: ".$_GET["data"];
?>
Tests
We can test the script by clicking on the send button.
PHP script
Now we will update the PHP server-side script. There are only few modifications to be
done:
server.php
<?php
echo "data recieved: ".$_POST["data"];
?>
Tests
We can test the script by clicking on the send button.
?>
As you can see, the most common use for get_class() is when one object can be of
several possible types, as in the code above. C++ users will be familiar with the concept
of Runtime Type Information (RTTI), and this is pretty much the same thing.