{"id":14519,"date":"2016-09-12T16:15:26","date_gmt":"2016-09-12T13:15:26","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=14519"},"modified":"2018-01-09T10:29:23","modified_gmt":"2018-01-09T08:29:23","slug":"ajax-php-tutorial","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/","title":{"rendered":"AJAX PHP Tutorial"},"content":{"rendered":"<p>AJAX is a technology that allows to establish asynchronous communication between the client and the server. That is, to allow a communication between the two points in the background.<\/p>\n<p>In this tutorial we will see how to use this technology, that involves JavaScript, which PHP in the server side.<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n[ulp id=&#8217;8njY7i2QRy6sg8pg&#8217;]<br \/>\n&nbsp;<br \/>\nFor this tutorial, we will use:<\/p>\n<ul>\n<li>Linux Mint (17.03) as Operating System.<\/li>\n<li>Apache HTTP server (2.4.7).<\/li>\n<li>PHP (5.5.9).<\/li>\n<li>MySQL (5.7.13), for a complete example later.<\/li>\n<\/ul>\n<div class=\"toc\">\n<h3>Table of contents<\/h3>\n<dl>\n<dt><a href=\"#section_1\">1. Preparing the environment<\/a><\/dt>\n<dt><a href=\"#section_2\">2. What is AJAX?<\/a><\/dt>\n<dt><a href=\"#section_3\">3. Using AJAX<\/a><\/dt>\n<dd><a href=\"#section_3_1\">3.1. Server side<\/a><\/dd>\n<dd><a href=\"#section_3_2\">3.2. Client side (without AJAX, yet)<\/a><\/dd>\n<dd><a href=\"#section_3_3\">3.3. Pure JavaScript<\/a><\/dd>\n<dd><a href=\"#section_3_4\">3.4. jQuery<\/a><\/dd>\n<dt><a href=\"#section_4\">4. Building a simple chat: a more thorough example<\/a><\/dt>\n<dd><a href=\"#section_4_1\">4.1. Directory structure<\/a><\/dd>\n<dd><a href=\"#section_4_2\">4.2. HTML<\/a><\/dd>\n<dd><a href=\"#section_4_3\">4.3. Stylesheet<\/a><\/dd>\n<dd><a href=\"#section_4_4\">4.4. Database<\/a><\/dd>\n<dd><a href=\"#section_4_5\">4.5. PHP<\/a><\/dd>\n<dd><a href=\"#section_4_6\">4.6. JavaScript<\/a><\/dd>\n<dd><a href=\"#section_4_7\">4.7. Result<\/a><\/dd>\n<dt><a href=\"#section_5\">5. Summary<\/a><\/dt>\n<dt><a href=\"#section_6\">6. Download the source code<\/a><\/dt>\n<\/dl>\n<\/div>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip environment preparation and jump directly to the <a href=\"#section_2\"><strong>beginning of the tutorial<\/strong><\/a> below.<\/div>\n<h2 id=\"section_1\">1. Preparing the environment<\/h2>\n<p>Below, commands to install Apache, PHP and MySQL are shown:<\/p>\n<pre class=\"brush:bash\">sudo apt-get update\r\nsudo apt-get install apache2 php5 libapache2-mod-php5 mysql-server php5-mysql\r\nsudo service apache2 restart<\/pre>\n<p><strong>Note:<\/strong> if you want to use Windows, installing XAMPP is the fastest and easiest way to install a complete web server that meets the prerequisites.<\/p>\n<h2 id=\"section_2\">2. What is AJAX?<\/h2>\n<p>Before diving in AJAX, let&#8217;s see<\/p>\n<p>When we make a request from the client to the server (e.g. a form submission), the information is sent from to the server, the server performs the required actions, and the client waits until the server generates an answer, which is sent back to it.<\/p>\n<p>The key here is that the client has to <em>wait<\/em>. This communication is made in a synchronous way. That is, when the client makes a request, the server takes the control, and it can&#8217;t do anything until the server sends the response.<\/p>\n<p>This can suppose a notable deterioration in the user experience for some cases.<\/p>\n<p>As a solution for this, AJAX was designed. AJAX stands for <em><strong>A<\/strong>synchronous <strong>J<\/strong>avaScript <strong>A<\/strong>nd <strong>X<\/strong>ML<\/em>, although it&#8217;s not restricted only to XML. With AJAX, the client can make requests that are processed by the server but in the background, without losing the control; i.e., in an asynchronous way.<\/p>\n<p>We find find several examples of AJAX in almost any website:<\/p>\n<ul>\n<li>Google, with its search autocompletion.<\/li>\n<li>Facebook, when we like something, and sends the information to the server without loading other page.<\/li>\n<li>Twitter, with the user search suggestion.<\/li>\n<li>Any forum, that checks that a username is available while we are typing it.<\/li>\n<li>And a large etc.<\/li>\n<\/ul>\n<p>Remember, <strong>AJAX is not a language, but a technology<\/strong>. Actually, is the combination of several technologies.<\/p>\n<h2 id=\"section_3\">3. Using AJAX<\/h2>\n<p>There&#8217;s more than a way to use AJAX in our websites, but all of them involve, obviously, JavaScript. The two main ways are:<\/p>\n<ul>\n<li>Pure JavaScript, with <code>XMLHttpRequest<\/code> object.<\/li>\n<li>jQuery, which offers other three ways.<\/li>\n<\/ul>\n<p>The general recommendation is to use jQuery, since it is much more easier to both code and read. But we should also know how AJAX works at the lowest level, in order to understand it properly.<\/p>\n<p>In this section, we will see how to deal with AJAX in every different way.<\/p>\n<h3 id=\"section_3_1\">3.1. Server side<\/h3>\n<p>Before we start with the client side that makes the asynchronous request, we will design a server-side page that generates an output.<\/p>\n<p>We will do a very simple page, that receives a name via GET method, and then greets politely the user. It would be as simple as the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>greeting.php<\/em><\/span><\/p>\n<pre class=\"brush:php;highlight:[10]\">&lt;?php\r\n\r\nif (isset($_GET['name'])) {\r\n\u00a0\u00a0\u00a0 $name = $_GET['name'];\r\n\u00a0\u00a0\u00a0 $response = \"Hello, $name, using AJAX!\";\r\n} else {\r\n\u00a0\u00a0\u00a0 $response = 'No name was specified.';\r\n}\r\n\r\necho $response;<\/pre>\n<p>Note that the script doesn&#8217;t do anything special. <strong>A PHP script that will be invoked via AJAX doesn&#8217;t need any particular change, just generate a response to the client<\/strong>. In this case, with a simple echo, in line 10.<\/p>\n<h3 id=\"section_3_2\">3.2. Client side (without AJAX, yet)<\/h3>\n<p>We have a server side script that generates an output for a GET parameter. So, we could design a simple form like the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>form.html<\/em><\/span><\/p>\n<pre class=\"brush:html\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n\u00a0\u00a0\u00a0 &lt;meta charset=\"UTF-8\"&gt;\r\n\u00a0\u00a0\u00a0 &lt;title&gt;AJAX form&lt;\/title&gt;\r\n\u00a0\u00a0\u00a0 &lt;script src=\"1_pure_javascript\/ajax.js\"&gt;&lt;\/script&gt;\r\n\u00a0\u00a0\u00a0 &lt;style type=\"text\/css\"&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .form-input {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 margin-bottom: 20px;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 &lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\u00a0\u00a0\u00a0 &lt;div id=\"ajax-form\"&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;div class=\"form-input\"&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;label for=\"name\"&gt;Name&lt;\/label&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;input type=\"text\" name=\"name\" id=\"name\"&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/div&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;div class=\"form-input\"&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;button id=\"form-submit\" onclick=\"sendAjaxRequest()\"&gt;Do magic!&lt;\/button&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/div&gt;\r\n\u00a0\u00a0\u00a0 &lt;\/div&gt;\r\n\r\n\u00a0\u00a0\u00a0 &lt;div id=\"ajax-response\"&gt;&lt;\/div&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>Note that it&#8217;s not an actual form. With a real form, we would establish a synchronous communication, since we would have to wait until the response is generated by the server. And the aim is to receive the answer asynchronously, generated by the server in the background.<\/p>\n<h3 id=\"section_3_3\">3.3. Pure JavaScript<\/h3>\n<p>The following is the JavaScript code that would make the AJAX request to the server side script, and then update the page:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>ajax.js<\/em><\/span><\/p>\n<pre class=\"brush:javascript;highlight:[2,6,7,9,13,14]\">function sendAjaxRequest() {\r\n\u00a0\u00a0\u00a0 var request = new XMLHttpRequest(),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 targetPage = 'greeting.php?name=',\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sendValue = document.getElementById('name').value;\r\n\r\n\u00a0\u00a0\u00a0 request.onreadystatechange = function() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (request.readyState === 4 &amp;&amp; request.status === 200) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var responseElement = document.getElementById('ajax-response');\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 responseElement.innerHTML = request.responseText;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 request.open('GET', targetPage + sendValue, true);\r\n\u00a0\u00a0\u00a0 request.send();\r\n}<\/pre>\n<p>Let&#8217;s examine it carefully:<\/p>\n<ul>\n<li>The first thing we have done is to instantiate the previously mentioned <code>XMLHttpRequest<\/code> class, in line 2, necessary for the AJAX request. Don&#8217;t worry, <a href=\"https:\/\/www.w3.org\/TR\/XMLHttpRequest\/\">is a W3C standard<\/a>, and works for every browser (or is supposed to do), even for Internet Explorer, from IE6 version.<\/li>\n<li>In line 6, we are assigning a piece of code to execute every time the request state changes. The request suffers changes on its state several times from the very beginning, to its end.<\/li>\n<li>The first thing we do inside the event, in line 7, is to check two things: the state and the status of the request. We have talked about the state in the previous point. The state identified by <code>4<\/code> is the one that means that the request is finished, and that the response from the server is ready. On the other hand, the <code>status<\/code> attribute of the object is the HTTP status of the request. And, as you will probably already know, the <code>200<\/code> status means OK.<\/li>\n<li>In line 9, we are retrieving the response from the server, accessing the <code>responseText<\/code> property of the <code>XMLHttpRequest<\/code> object. Remember that the <code>responseText<\/code> should only be accessed when the request is finished and ready, and the HTTP status is 200, as we have done.<\/li>\n<li>Once defined the event to be executed for the state change, we can configure the request. For this, we have to call the <code>open()<\/code> method, like in line 13, specifying the method (GET or POST); the request values, if any, in <code>key1=value1&amp;keyN=valueN<\/code> format; and telling that is an asynchronous request (with <code>true<\/code>).<\/li>\n<li>Finally, we send the defined request.<\/li>\n<\/ul>\n<h3 id=\"section_3_4\">3.4. jQuery<\/h3>\n<p>As said before, jQuery library offers AJAX methods that make easier its use.<\/p>\n<p>There are several methods, but the main ones for making AJAX requests are the following three:<\/p>\n<ul>\n<li><code>$.get()<\/code>, for GET requests.<\/li>\n<li><code>$.post()<\/code>, for POST requests.<\/li>\n<li><code>.$ajax()<\/code>, for both.<\/li>\n<\/ul>\n<p>Before starting, we need to include the jQuery source in the HTML page:<\/p>\n<pre class=\"brush:html\">&lt;head&gt;\r\n\u00a0\u00a0\u00a0 &lt;script src=\"https:\/\/code.jquery.com\/jquery-1.12.4.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;!-- ... ---&gt;\r\n&lt;\/head&gt;<\/pre>\n<h4>3.4.1. $.get()<\/h4>\n<p>The following JavaScript code is for sending a GET request with <code>$.get()<\/code> method:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>ajax.js<\/em><\/span><\/p>\n<pre class=\"brush:javascript; highlight:[5,6]\">$(document).ready(function() {\r\n\u00a0\u00a0\u00a0 $('#form-submit').click(function() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var sendValue = $('#name').val();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $.get('greeting.php', { 'name' : sendValue }, function(data, status) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (status === 'success') {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $('#ajax-response').html(data);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 })\r\n\u00a0\u00a0\u00a0 });\r\n});<\/pre>\n<p>As we can see, is very simple:<\/p>\n<ul>\n<li>The first parameter is the server side page the request will be sent to.<\/li>\n<li>The second parameter is a object literal with the values that we want to send with the request.<\/li>\n<li>Finally, a callback function with the received data from the server, and the status of the request.<\/li>\n<\/ul>\n<p>Note that we should check if the status indicates an error in the request, to act accordingly.<\/p>\n<h4>3.4.2. $.post()<\/h4>\n<p>For POST requests, is exactly the same as with GET, but calling <code>$.get()<\/code> method:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>ajax.js<\/em><\/span><\/p>\n<pre class=\"brush:javascript\">$(document).ready(function() {\r\n\u00a0\u00a0\u00a0 $('#form-submit').click(function() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var sendValue = $('#name').val();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $.post('greeting.php', { 'name' : sendValue }, function(data, status) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (status === 'success') {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $('#ajax-response').html(data);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 })\r\n\u00a0\u00a0\u00a0 });\r\n});<\/pre>\n<h4>3.4.3. $.ajax()<\/h4>\n<p>Finally, there&#8217;s a third method. The <code>$.ajax()<\/code> method is, probably, the <em>best<\/em> option. Let&#8217;s see an example:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>ajax.js<\/em><\/span><\/p>\n<pre class=\"brush:javascript\">$(document).ready(function() {\r\n\u00a0\u00a0\u00a0 $('#form-submit').click(function() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var sendValue = $('#name').val();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $.ajax({\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 url: 'greeting.php',\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data: {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 'name' : sendValue\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 },\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 type: 'GET',\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 success: function(data) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $('#ajax-response').html(data);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 },\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error: function(xhr, status) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ ...\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 });\r\n\u00a0\u00a0\u00a0 });\r\n});<\/pre>\n<p>As we can see, this can be considered tidier, more readable and maintainable, since we use a object literal for each element, and because we specify the method with a string, so we could use a variable.<\/p>\n<h2 id=\"section_4\">4. Building a simple chat: a more thorough example<\/h2>\n<p>We have seen pages with a simple AJAX request, just printing a hardcoded message. In this section, we will see a more realistic example, including a database. For this, we will build a simple chat.<\/p>\n<h3 id=\"section_4_1\">4.1. Directory structure<\/h3>\n<p>Before starting, let&#8217;s see the directory structure that we will follow:<\/p>\n<pre class=\"brush:bash\">.\r\n\u251c\u2500\u2500 css\r\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 styles.css\r\n\u251c\u2500\u2500 database.sql\r\n\u251c\u2500\u2500 index.html\r\n\u251c\u2500\u2500 js\r\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 ajax.js\r\n\u2514\u2500\u2500 php\r\n\u00a0\u00a0\u00a0 \u251c\u2500\u2500 db.php\r\n\u00a0\u00a0\u00a0 \u251c\u2500\u2500 readMessages.php\r\n\u00a0\u00a0\u00a0 \u2514\u2500\u2500 saveMessage.php<\/pre>\n<h3 id=\"section_4_2\">4.2. HTML<\/h3>\n<p>The HTML can be extremely simple:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>index.html<\/em><\/span><\/p>\n<pre class=\"brush:html\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n\u00a0\u00a0\u00a0 &lt;meta charset=\"UTF-8\"&gt;\r\n\u00a0\u00a0\u00a0 &lt;title&gt;AJAX chat&lt;\/title&gt;\r\n\u00a0\u00a0\u00a0 &lt;script src=\"https:\/\/code.jquery.com\/jquery-1.12.4.min.js\"&gt;&lt;\/script&gt;\r\n\u00a0\u00a0\u00a0 &lt;script src=\"js\/ajax.js\"&gt;&lt;\/script&gt;\r\n\u00a0\u00a0\u00a0 &lt;link rel=\"stylesheet\" type=\"text\/css\" href=\"css\/styles.css\"&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\u00a0\u00a0\u00a0 &lt;div id=\"chat-wrapper\"&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;div id=\"chat-messages\"&gt;&lt;\/div&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;form id=\"chat-form\" action=\"#\" method=\"POST\"&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;input type=\"text\" id=\"chat-input\"&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/form&gt;\r\n\u00a0\u00a0\u00a0 &lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>We just need a div to display the messages, and an input for sending the messages.<\/p>\n<h3 id=\"section_4_3\">4.3. Stylesheet<\/h3>\n<p>Just the simplest styling for the chat:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>styles.css<\/em><\/span><\/p>\n<pre class=\"brush:css\">#chat-wrapper {\r\n\u00a0\u00a0\u00a0 width: 60%;\r\n\u00a0\u00a0\u00a0 margin: auto;\r\n}\r\n\r\n#chat-messages {\r\n\u00a0\u00a0\u00a0 border: 1px solid gray;\r\n\u00a0\u00a0\u00a0 height: 400px;\r\n}\r\n\r\n#chat-input {\r\n\u00a0\u00a0\u00a0 width: 99%;\r\n\u00a0\u00a0\u00a0 height: 30px;\r\n}\r\n\r\n.chat-message {\r\n\u00a0\u00a0\u00a0 display: block;\r\n}\r\n\r\n.chat-message .ip {\r\n\u00a0\u00a0\u00a0 font-weight: bold;\r\n}<\/pre>\n<h3 id=\"section_4_4\">4.4. Database<\/h3>\n<p>We will have a very simple database, with a single table, with two columns. Apart from the messages, we will save the IP of the sender, to be able to identify each message.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>database.sql<\/em><\/span><\/p>\n<pre class=\"brush:sql\">CREATE DATABASE IF NOT EXISTS chat;\r\n\r\nGRANT ALL ON `chat`.* TO 'chat'@'localhost' IDENTIFIED BY 'chat'; -- Creates user if it does not exist yet.\r\n\r\nUSE chat;\r\n\r\nCREATE TABLE messages(\r\n\u00a0\u00a0\u00a0 message VARCHAR(1024),\r\n\u00a0\u00a0\u00a0 ip\u00a0\u00a0\u00a0\u00a0\u00a0 VARCHAR(15)\r\n);<\/pre>\n<p>To apply it, just access the MySQL command line, e.g., with root:<\/p>\n<pre class=\"brush:bash\">mysql --user=root --password<\/pre>\n<p>After logging in correctly, paste the SQL sentences.<\/p>\n<h3 id=\"section_4_5\">4.5. PHP<\/h3>\n<p>In this section, we will see the part of the application that communicates with the database.<\/p>\n<p>We will create a single script for establishing the connection:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>db.php<\/em><\/span><\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\ndefine('HOST', 'localhost');\r\ndefine('USERNAME', 'chat');\r\ndefine('PASSWORD', 'chat');\r\ndefine('DATABASE', 'chat');\r\ndefine('PORT', 3306);\r\n\r\n\/**\r\n\u00a0* Creates the connection to the database.\r\n\u00a0*\r\n\u00a0* @return Connection object.\r\n\u00a0*\/\r\nfunction connect() {\r\n\u00a0\u00a0\u00a0 $connection = mysqli_connect(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 HOST,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 USERNAME,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 PASSWORD,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DATABASE,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 PORT\r\n\u00a0\u00a0\u00a0 );\r\n\r\n\u00a0\u00a0\u00a0 return $connection;\r\n}<\/pre>\n<p>Then, we will have two scripts, one for saving the messages, and another for retrieving them.<\/p>\n<p>The following script is for saving the messages, which, as you can see, only makes an insertion of the received message via POST method, and the IP of the client.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>saveMessage.php<\/em><\/span><\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\nrequire_once('db.php');\r\n\r\n\/**\r\n\u00a0* Creates a row in the messages table.\r\n\u00a0*\r\n\u00a0* @param $connection The connection to the database.\r\n\u00a0* @param string $message The message to save.\r\n\u00a0* @param string $ip The ip of the sender.\r\n\u00a0*\/\r\nfunction insertMessage($connection, $message, $ip) {\r\n\u00a0\u00a0\u00a0 $sql = 'INSERT INTO messages (message, ip) '\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 . 'VALUES (?, ?)';\r\n\r\n\u00a0\u00a0\u00a0 $statement = $connection-&gt;prepare($sql);\r\n\u00a0\u00a0\u00a0 $statement-&gt;bind_param('ss', $message, $ip);\r\n\r\n\u00a0\u00a0\u00a0 $statement-&gt;execute();\r\n}\r\n\r\n\/\/ Flow starts here.\r\n\r\n$message = $_POST['message'];\r\n$ip = $_SERVER['REMOTE_ADDR'];\r\n\r\n$connection = connect();\r\n\r\ninsertMessage($connection, $message, $ip);\r\n\r\nmysqli_close($connection);<\/pre>\n<p>And, then, we need to read them. We just have to iterate through the records of the table, constructing the response, like in the following script.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>readMessages.php<\/em><\/span><\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\nrequire_once('db.php');\r\n\r\n\/**\r\n\u00a0* Reads the messages from the database.\r\n\u00a0*\r\n\u00a0* @param $connection The connection to the database.\r\n\u00a0* @return string The existing messages in HTML format.\r\n\u00a0*\/\r\nfunction readMessages($connection) {\r\n\u00a0\u00a0\u00a0 $sql = 'SELECT message, ip '\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 . 'FROM messages';\r\n\r\n\u00a0\u00a0\u00a0 $result = mysqli_query($connection, $sql);\r\n\r\n\u00a0\u00a0\u00a0 $messages = '';\r\n\r\n\u00a0\u00a0\u00a0 while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $message = '&lt;span class=\"chat-message\"&gt;';\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $message .= '&lt;span class=\"ip\"&gt;' . $row[1] . '&lt;\/span&gt;: ';\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $message .= $row[0];\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $message .= '&lt;\/span&gt;';\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $messages .= $message;\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 return $messages;\r\n}\r\n\r\n\/**\r\n\u00a0* Deletes the messages from the database, once read.\r\n\u00a0*\r\n\u00a0* @param $connection Connection to the database.\r\n\u00a0*\/\r\nfunction deleteMessages($connection) {\r\n\u00a0\u00a0\u00a0 $sql = 'DELETE FROM messages';\r\n\r\n\u00a0\u00a0\u00a0 mysqli_query($connection, $sql);\r\n}\r\n\r\n\/\/ Flow starts here.\r\n\r\n$connection = connect();\r\n\r\n$messages = readMessages($connection);\r\ndeleteMessages($connection);\r\nmysqli_close($connection);\r\n\r\necho $messages;<\/pre>\n<p>After creating the response, we delete the existing records, not to repeat the response.<\/p>\n<h3 id=\"section_4_6\">4.6. JavaScript<\/h3>\n<p>Finally, we need the JavaScript code for the AJAX requests. We will see that, actually, is pretty easy:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>ajax.js<\/em><\/span><\/p>\n<pre class=\"brush:javascript;highlight:[3]\">$(document).ready(function() {\r\n\u00a0\u00a0\u00a0 $('#chat-form').submit(function(event) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 event.preventDefault();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var message = $('#chat-input').val();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sendMessage(message);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $('#chat-input').val('');\r\n\u00a0\u00a0\u00a0 });\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * Sends the message to the server.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * @param String message The message introduced by the user.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 function sendMessage(message) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $.ajax({\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 url: 'php\/saveMessage.php',\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data: {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 'message' : message\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 },\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 type: 'POST'\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 });\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * Reads the messages saved in the server, and displays them.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 function readMessages() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $.ajax({\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 url: 'php\/readMessages.php',\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 type: 'GET',\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 success: function(data) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 addMessage(data);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 });\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * Adds a message to the messages div.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * @param String message The message to add.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 function addMessage(message) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 message = '&lt;span class=\"chat-message\"&gt;' + message + '&lt;\/span&gt;';\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $('#chat-messages').append(message);\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 setInterval(function() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 readMessages();\r\n\u00a0\u00a0\u00a0 }, 100);\r\n});<\/pre>\n<p>We can see that there&#8217;s no difficulty. We just need to make a request for sending the message to the server, which is triggered on form submit; and another to retrieve them, for which we need to set an interval, in order to update the displayed messages automatically.<\/p>\n<p>As said before, we should take care of failed request, to try them again, notify the user about it, etc.<\/p>\n<p>An important point in this script is that we prevent the default form behavior (line 3), because, otherwise, the form will submit, and a synchronous communication would start (as said in 3.2. section).<\/p>\n<h3 id=\"section_4_7\">4.7. Result<\/h3>\n<p>The following is a screenshot of how looks the chat:<\/p>\n<figure style=\"width: 816px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/09\/chat.jpg\" width=\"816\" height=\"446\" \/><figcaption class=\"wp-caption-text\">1. Chat output example.<\/figcaption><\/figure>\n<h2 id=\"section_5\">5. Summary<\/h2>\n<p>This tutorial has shown how to use AJAX technology to establish an asynchronous communication with the server, where we have used PHP. We have started from the lowest level, with pure JavaScript, to see how the AJAX internals work; and then more appropriate solutions, in terms of difficulty, readability and maintainability, with jQuery, seeing every different option. Finally, we have built a simple chat, to look at AJAX in a more realistic scenario.<\/p>\n<h2 id=\"section_6\">6. Download the source code<\/h2>\n<p>This was a tutorial of AJAX and PHP.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/09\/AJAXPHPTutorial.zip\"><strong>AJAXPHPTutorial<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>AJAX is a technology that allows to establish asynchronous communication between the client and the server. That is, to allow a communication between the two points in the background. In this tutorial we will see how to use this technology, that involves JavaScript, which PHP in the server side. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &hellip;<\/p>\n","protected":false},"author":160,"featured_media":930,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[34,82,122],"class_list":["post-14519","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-ajax","tag-javascript","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>AJAX PHP Tutorial - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"AJAX is a technology that allows to establish asynchronous communication between the client and the server. That is, to allow a communication between the\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"AJAX PHP Tutorial - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"AJAX is a technology that allows to establish asynchronous communication between the client and the server. That is, to allow a communication between the\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-09-12T13:15:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T08:29:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Toni\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Toni\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/\"},\"author\":{\"name\":\"Toni\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966\"},\"headline\":\"AJAX PHP Tutorial\",\"datePublished\":\"2016-09-12T13:15:26+00:00\",\"dateModified\":\"2018-01-09T08:29:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/\"},\"wordCount\":1683,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"keywords\":[\"Ajax\",\"javascript\",\"php\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/\",\"name\":\"AJAX PHP Tutorial - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"datePublished\":\"2016-09-12T13:15:26+00:00\",\"dateModified\":\"2018-01-09T08:29:23+00:00\",\"description\":\"AJAX is a technology that allows to establish asynchronous communication between the client and the server. That is, to allow a communication between the\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/php\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"AJAX PHP Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966\",\"name\":\"Toni\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g\",\"caption\":\"Toni\"},\"url\":\"https:\/\/www.webcodegeeks.com\/author\/julen-pardo\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"AJAX PHP Tutorial - Web Code Geeks - 2026","description":"AJAX is a technology that allows to establish asynchronous communication between the client and the server. That is, to allow a communication between the","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"AJAX PHP Tutorial - Web Code Geeks - 2026","og_description":"AJAX is a technology that allows to establish asynchronous communication between the client and the server. That is, to allow a communication between the","og_url":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2016-09-12T13:15:26+00:00","article_modified_time":"2018-01-09T08:29:23+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","type":"image\/jpeg"}],"author":"Toni","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Toni","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/"},"author":{"name":"Toni","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966"},"headline":"AJAX PHP Tutorial","datePublished":"2016-09-12T13:15:26+00:00","dateModified":"2018-01-09T08:29:23+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/"},"wordCount":1683,"commentCount":1,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","keywords":["Ajax","javascript","php"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/","url":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/","name":"AJAX PHP Tutorial - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","datePublished":"2016-09-12T13:15:26+00:00","dateModified":"2018-01-09T08:29:23+00:00","description":"AJAX is a technology that allows to establish asynchronous communication between the client and the server. That is, to allow a communication between the","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/php\/ajax-php-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.webcodegeeks.com\/category\/php\/"},{"@type":"ListItem","position":3,"name":"AJAX PHP Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966","name":"Toni","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g","caption":"Toni"},"url":"https:\/\/www.webcodegeeks.com\/author\/julen-pardo\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/14519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/160"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=14519"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/14519\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/930"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=14519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=14519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=14519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}