All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Autocomplete Textbox Using jQuery,PHP And MySQL

Last Updated : Jul 1, 2023

IN - jQuery PHP MySQL | Written & Updated By - Riya

In this tutorial we will show you how to create autocomplete textbox using jQuery, PHP and MySQL. When user enter some letter a box with some related words will display and you can select the word from that box.

This is all done with the help of jQuery UI you have to download it to create autocomplete textbox.You may also like disable auto complete form using jQuery.

Autocomplete Textbox Using jQuery,PHP And MySQL

To Create Autocomplete Textbox It Takes Only Two Steps:-

  1. Make a HTML file and define markup and scripting
  2. Make a PHP file to send related word suggestion

Step 1. Make a HTML file and define markup and scripting

We make a HTML file and save it with a name textbox.html

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery-ui.js"></script>
<link rel="stylesheet" href="jquery-ui.css">
<script type="text/javascript">
$(function() 
{
 $( "#coding_language" ).autocomplete({
  source: 'autocomplete.php'
 });
});
</script>
</head>
<body>
<div id="wrapper">

<div class="ui-widget">
 <p>Enter Coding Language</p>
 <input type="text" id="coding_language">
</div>

</div>
</body>
</html>

In this step we first insert all the files required for the working of autocomplete textbox and then create a textbox to enter text and jQuery autocomplete function to show related words for that text.

In autocomplete() function there is parameter 'source' it takes page address from where you will get word suggestions under this function we use 'autocomplete.php' file which we were going to create in next step.

You may also like dynamic textbox using Javascript.

Step 2. Make a PHP file to send related word suggestion

We make a PHP file and save it with a name autocomplete.php

<?php
// Database Structure 
CREATE TABLE `coding_language` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` text NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

$host="localhost";
$username="root";
$password="";
$databasename="sample";

$connect=mysql_connect($host,$username,$password);
$db=mysql_select_db($databasename);

$searchTerm = $_GET['term'];

$select =mysql_query("SELECT * FROM coding_language WHERE name LIKE '%".$searchTerm."%'");
while ($row=mysql_fetch_array($select)) 
{
 $data[] = $row['name'];
}
//return json data
echo json_encode($data);
?>

In this step we create a sample table called 'coding_language' and insert some web programming languages and then get the term from 'textbox.html' page and then find and send the related languages.

You can view our form validation using jQuery to filter data before querying the database.

Thats all, this is how to create autocomplete textbox using jQuery, PHP and MySQL. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on autocomplete textbox jquery helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪