How To Create Autocomplete In TextBox Using PHP
Submitted by alpha_luna on Monday, April 18, 2016 - 10:43.
If you are looking for a source code on How To Create Autocomplete In TextBox Using PHP then you are at the right place. This tutorial can provide you an auto-suggestion or autocomplete in the textbox for all users while you typing in the textbox.
In this case, we are going to suggest the Province of the Philippines from the database that starts with the keyword typing by the user.
TextBox Field - HTML
This source code contains textbox, and this is for autocomplete function.
Database Using PHP
This is our PHP Script for our database.
And, this is the output.
Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.
Autocomplete Script - jQuery
This script will help us to do the function of autocomplete.
- <script src="js/code_js.js" type="text/javascript"></script>
- <script>
- $(document).ready(function(){
- $("#search_textBox").keyup(function(){
- $.ajax({
- type: "POST",
- url: "list_of_province.php",
- data:'keyword='+$(this).val(),
- beforeSend: function(){
- $("#search_textBox").css("background","#FFF url(LoaderIcon.gif) no-repeat 165px");
- },
- success: function(data){
- $("#suggestion_list").show();
- $("#suggestion_list").html(data);
- $("#search_textBox").css("background","#FFF");
- }
- });
- });
- });
- function selectCountry(val) {
- $("#search_textBox").val(val);
- $("#suggestion_list").hide();
- }
- </script>
- <?php
- require_once("db.php");
- $db_handle = new DB();
- $query ="SELECT * FROM province WHERE province_name like '" . $_POST["keyword"] . "%' ORDER BY province_name LIMIT 0,6";
- $result = $db_handle->runQuery($query);
- ?>
- <ul id="province_list">
- <?php
- foreach($result as $country) {
- ?>
- <li onClick="selectCountry('<?php echo $country["province_name"]; ?>');"><?php echo $country["province_name"]; ?></li>
- <?php } ?>
- </ul>
- <?php } } ?>

Add new comment
- 358 views