PHP MySQLi jQuery UI Autocomplete Database Search
June 17, 2022 by Sanjay Kumar
Table of Contents
Share this Article
Reading Time: 7 minutes 54 Views
Inside this article we will see the concept of autocomplete search using jquery ui PHP MySQLi. This autocomplete search will be total dynamic which searches data from database.
You will get the complete concept of PHP MySQLi jQuery UI Autocomplete Database Search. Autocomplete is a jQuery UI plugin feature which gives the flexibility to search value from a list of values.
Article contains classified information about Autocomplete Database Search of jQuery UI in PHP MySQLi. It will help to create this advance feature.
Learn More –
Let’s get started.
Create Database & Table
To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.
Copy CREATE DATABASE php_applications;
Inside this database, we need to create a table.
Tables we need – countries .
Copy CREATE TABLE `countries` (
`id` int(10) UNSIGNED NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`sortname` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`phonecode` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Dummy Data for Application
Here, you need to copy mysql query from this given file and run into sql tab of phpmyadmin. First download it and run.
Download Countries Data
You will see something like this after this test data insertion.
Application Folder Structure
You need to create a folder structure to develop this application in PHP and MySQLi. Have a look the files and folders inside this application –
Create a folder with name php-autocomplete and create these 3 files into it.
Database Configuration
Open dbconfig.php file from folder. Add these lines of code into it.
dbconfig.php
Copy @Author: Sanjay Kumar
Copy @Project: PHP & MySQLi Autocomplete Search From Database Using jQuery UI
Copy @Email: onlinewebtutorhub@gmail.com
Copy @Website: https://onlinewebtutorblog.com/
Copy // Database configuration
Copy $dbpass = "Admin@123";
Copy $dbname = "php_applications";
Copy // Create database connection
Copy $conn = new mysqli($host, $dbuser, $dbpass, $dbname);
Copy if ($conn->connect_error) {
Copy die("Connection failed: " . $conn->connect_error);
Frontend Dropdown Layout
Open index.php file from application. Add these lines of code into it.
index.php
Copy @Author: Sanjay Kumar
Copy @Project: PHP & MySQLi Autocomplete Search From Database Using jQuery UI
Copy @Email: onlinewebtutorhub@gmail.com
Copy @Website: https://onlinewebtutorblog.com/
Copy // Include the functions file
Copy require 'functions.php';
Copy <title>PHP & MySQLi Autocomplete Search From Database Using jQuery UI</title>
Copy <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
Copy <!-- jQuery UI CSS -->
Copy <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
Copy <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Copy <!-- jQuery UI JS -->
Copy <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
Copy <div class="container" style="margin-top: 40px;">
Copy <div class="panel panel-primary">
Copy <div class="panel-heading">PHP & MySQLi Autocomplete Search From Database Using jQuery UI</div>
Copy <div class="panel-body">
Copy <label>Search Country...</label>
Copy <input class="form-control" id="autocomplete_country" type="text">
Copy <p>Country ID : <span id="countryId"></span></p>
Copy <script type='text/javascript'>
Copy $(document).ready(function() {
Copy $("#autocomplete_country").autocomplete({
Copy source: function(request, response) {
Copy url: "functions.php",
Copy search: request.term
Copy success: function(data) {
Copy response(data.data);
Copy select: function(event, ui) {
Copy $('#autocomplete_country').val(ui.item.label); // display the selected text
Copy $('#countryId').text(ui.item.value); // save selected id to input
Copy focus: function(event, ui) {
Copy $("#autocomplete_country").val(ui.item.label);
Copy $("#countryId").text(ui.item.value);
Ajax Handler & Functions
Open functions.php file. Add these lines of it.
functions.php
Copy @Author: Sanjay Kumar
Copy @Project: PHP & MySQLi Autocomplete Search From Database Using jQuery UI
Copy @Email: onlinewebtutorhub@gmail.com
Copy @Website: https://onlinewebtutorblog.com/
Copy // Include the database configuration file
Copy require 'dbconfig.php';
Copy // Get data via ajax
Copy $search = isset($_POST['search']) && !empty($_POST['search']) ? $_POST['search'] : "";
Copy $itemRecords = array();
Copy if (!empty($search)) {
Copy $countrySelect = $conn->prepare("SELECT * FROM countries WHERE name LIKE CONCAT('%', ?, '%')");
Copy $countrySelect->bind_param("s", $search);
Copy $countrySelect = $conn->prepare("SELECT * FROM countries");
Copy $countrySelect->execute();
Copy $countries = $countrySelect->get_result();
Copy while ($item = $countries->fetch_assoc()) {
Copy $itemDetails = array(
Copy array_push($itemRecords, $itemDetails);
Copy echo json_encode(array(
Copy "data" => $itemRecords
Application Testing
Now,
URL: http://localhost/php-autocomplete/index.php
Download Complete Source Code
Download Source Code
We hope this article helped you to PHP MySQLi jQuery UI Autocomplete Database Search in a very detailed way.
Buy Me a Coffee
Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook .