Ajax usage to search a Record with PHP, MySQL and jQuery

I have created a few post about how to Search, Update and Delete data from a MySQL Database with PHP and PDO. Those tutorials are quite simple (but not dynamically enough, they don’t use Ajax), but they are very important to catch the flow of this tutorial if you have never worked with this technologies. The tutorials are in Spanish so if you really need them in English I will be glad to translate them.

Something that I would like to remark is that you can get the scripts at the end of the tutorial.

In order to create a nice look and feel to the page I have implemented the framework Boostrap, but if you don’t want to focus on the look and feel you only have to focus on the Form, the event to Fire the Ajax request and the server to process your request.

The next flow chart will show the path that we are going to cover to complete our Ajax search:

Ajax flow chart. Click to enlarge !
Ajax flow chart.

The HTML form

So the first thing that we must have is our form. Our form will contain a Label, Input, and a Button. We are not going to create a submit button because in order to add a nice style we need to use the button tag instead. The events Submit of the form and Click will be handle by jQuery.

So this code will bring us the following form:

You will see that form in the picture looks nice, this is achieve by implementing the classes from Bootstrap, but in order to keep the a clean code in the tutorial I will avoid putting all the styles in the code, you can have access to all this in the source files that I will place down.

Adding jQuery Library

I will put the jQuery library at the bottom of the page. You can put it on the head tag since this is not a page with loaded content. Usually you use it at the bottom of the page when you want the page to load smoothly.

jQuery Library to use Ajax.
jQuery Library to use Ajax.

The Actions

We are going to handle the events with two jQuery functions (Submit and Click), this events are trigger with the search button and when the form is submitted (Since we don’t have a submit button the form will be submitted when the user press the key Enter)

The Ajax Request

Since we are going to make the Ajax request either on the Submit or Click event we have to reduce our code by creating a function. This function will handle the request for both events.

It is important to notice that from the server we get a response which will be use to show the result in a Table. Initialy we will have a table like this:

Ajax results on this Table. Click to enlarge !
Ajax results on this Table.

 

The structure of our table is:

As you can see I left the tag <tbody> empty, this tag will be filled with jQuery, so to review the jQuery Ajax properties this is a simplyfied explanation:

url: Is the path where we are sending the data.

type: How are we sending the data, post/get?

data: The data to be send in a query string format or JSON which is our case.

success: A callback function to be executed is the request was succesful. A ‘response’ variable will be returned.

Because I left the tag <tbody> with no data it will be fill with ‘response’. To achieve this we us jQuery html() method to add HTML code to this matched selector. The body of <tbody> will be a table structure created by the server. After we fill the table it will look like this:

Results loaded via Ajax. Click to enlarge !
Results loaded via Ajax

Creation of Our Table (employee)

With this script we create and insert data into our table in MySQL:

Server Side ‘search.php’ (PHP)

Now in our server side we have to do a few steps to get our Information:

  1. Create a Database Connection to MySQL with PDO.
  2. Verify that the information is coming from a GET request.
  3. Create the Query.
  4. Prepare the query with the parameters.
  5. Bind and Execute the Query.
  6. Fetch all the records.
  7. Validate results.
  8. If there are results create the structure of a HTML table to fill the <tbody> in the client side.

So the code that execute all the steps is:

With all these steps we can get our nice jQuery Ajax Search Application, at the end we add a nice touch to our application with Bootstrap to make it look more professional.

Click to enlarge !

Buy the full code with full code explanation(Recommended for beginners)