|Language:||English, Spanish, Hindi|
|Distribution:||Free* [*Sign up for free]|
In this section, we will see a more realistic example, including a database. For this, we will build a simple chat.
Stylesheet Just the simplest styling for the chat: styles. Database 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. PHP In this section, we will see the part of the application that communicates with the database. We will create a single script for establishing the connection: db. 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.
We just have to iterate through the records of the table, constructing the response, like in the following script. We will see that, actually, is pretty easy: ajax. When we make a request from the client to the server e.
The key here is that the client has to wait. This communication is made in a synchronous way.
As a solution for this, AJAX was designed. With AJAX, the client can make requests that are processed by the server but in the background, without losing the control; i. Remember, AJAX is not a language, but a technology. Actually, is the combination of several technologies.
The two main ways are:. 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.
Before we start with the client side that makes the asynchronous request, we will design a server-side page that generates an output. 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:. In this case, with a simple echo, in line We have a server side script that generates an output for a GET parameter.
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. 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. 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. 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.
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. We will see that, actually, is pretty easy:.
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. As said before, we should take care of failed request, to try them again, notify the user about it, etc.
Learn how your comment data is processed. Currently he is working in Munich, Germany, as Software Engineer. He contributes to open source projects with plugins, and he also develops his own, open-source projects.
Julen is continuously trying to learn and adopt Software Engineering principles and practices to build better, more secure, readable and maintainable software. Table of contents 1. Preparing the environment 2.