In this tutorial, we will create an Auto-Search using PHP. This code will auto search specific data in the MySQLi server when the user entered a keyword. The code use jQuery ajax to automatically search a table row data from the MySQLi server without a refreshing web page and modify the HTML table to display the result. This is a user-friendly kind of program feel free to change it and use it as your own.
We will be using jQuery, a JavaScript framework that designs to simplify HTML DOM tree traversal and manipulation. It can retrieve DOM and manipulate each property of an element based on different criteria such as id, class, etc.
Table of Contents
Getting Started:
First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server https://www.apachefriends.org/index.html.
And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.
Creating Database
Open your database web server then create a database name in it db_auto, after that, click Import, then locate the database file inside the folder of the application then click ok.
Creating the database connection
Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
<?php $conn = new mysqli('localhost', 'root', '', 'db_auto'); if(!$conn){ die("Error: Cam't connect to database!"); } ?>
Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
<!DOCTYPE html> <html lang="en"> <head> <head> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/> </head> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a> </div> </nav> <div class="col-md-3"></div> <div class="col-md-6 well"> <h3 class="text-primary">PHP - Auto Search Using jQuery</h3> <hr style="border-top:1px dotted #ccc;"/> <div class="col-md-4"> <form action="save.php" method="POST"> <div class="form-group"> <label>Firstname</label> <input type="text" class="form-control" name="firstname" required="required"/> </div> <div class="form-group"> <label>Lastname</label> <input type="text" class="form-control" name="lastname" required="required"/> </div> <div class="form-group"> <label>Address</label> <input type="text" class="form-control" name="address" required="required"/> </div> <center><button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Add Member</button></center> </form> </div> <div class="col-md-8"> <div class="form-group"> <input class="form-control" type="text" id="search" placeholder="Search here..."/> </div> <div id="result"></div> </div> </div> </body> <script src="js/jquery-3.2.1.min.js"></script> <script src="js/bootstrap.js"></script> <script src="js/script.js"></script> </html>
Creating the PHP Query
This code contains the php query of the application. This code will store the data inputs to the database server. To do this just copy and write these block of codes below inside the text editor, then save it as save.php.
<?php require_once 'conn.php'; if(ISSET($_POST['save'])){ $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $address = $_POST['address']; $conn->query("INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_errno()); header('location: index.php'); } ?>
Creating the Main Function
This code contains the main function of the application. This code will auto search a specific data in the database when the keyword is entered. To make this just copy and write these block of codes below inside the text editor, then save it as shown below.
auto_search.php
$(document).ready(function(){ $('#search').on('keyup', function(){ if($("#search").val() == ""){ $('#result').empty(); }else{ var search = $("#search").val(); $.ajax({ url: 'auto_search.php', type: 'POST', data: {search: search}, dataType: 'text', success: function(data){ $("#result").html(data); } }); } }); });
There you have it we successfully created Auto Search using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!