jQuery Autocomplete Search using PHP, MySQL and Ajax

jQuery Autocomplete Search using PHP, MySQL and Ajax

In this post we are going to attempting to provide the essential ans easy idea for jQuery Autocomplete Search using jquery, mythical being and PHP.

A straightforward and short php jQuery Autocomplete Search example. essentially this instance is intended for beginner and additionally for intermediate level developers.

jQuery Autocomplete Search principally need to provide self-hypnosis once anyone kind one thing within the search field or text field. during this example, we have a tendency to area unit aiming to counsel name beginning with Start search with your name.

This jQuery Autocomplete Search example provide you with the thought to make this sort of feature in your net application in future.

jQuery Autocomplete Search using PHP – onlinecode-org

So this jQuery Autocomplete Search using ajax example divided into three parts.

  • First need a autocomplete text box for search.
  • Second ajax function to send value and find the result.
  • Last we want to fetch autocomplete suggestion data from database and append in our result space.
    Take a fast verify this live demo to grasp the flow of the autocomplete search using jQuery, Ajax, PHP. additionally you’ll be able to transfer this script from here and may strive in your localhost or live server wherever you wish to try.

create dbconfig.php file for database connection.

<?php
    // connect database
	$user_host = 'localhost';
	$user_name = 'root';
	$pass_word = '';
	$data_base = 'test'; //you can use your database name.
	// connect db with mysqli connect
	$dbconfig = mysqli_connect($user_host,$user_name,$pass_word,$data_base);
?>

create index.php the form where user input to get jQuery Autocomplete Search using PHP, MySQL and Ajax

<!-- index.php   -->
<div class="search-frm">
	<form name="search-example" method="" action="">
		<input type="text" id="searchword" placeholder="Start search with your name" />
		// search input form for type
	</form>
	<div id="id_suggesstions">
		// this area for show the jquery ajax autocomplete search result
	</div>
</div>

jquery ajax function call when anything enter by user

<!-- javascript  of index.php   -->
<script>
$(document).ready(function(){
    // when any character press on the input field keyup function call
    $("#searchword").keyup(function(){
        $.ajax({
        type: "POST", // here used post method
        url: "readname.php",//php file where retrive the post value and fetch all the matched item from database
        data:'searchterm='+$(this).val(),//send data or search term to readname file to process
        beforeSend: function(){
            // show loader icon
            $("#searchword").css("background","#FFF url(LoaderIcon.gif) no-repeat 175px");
        },
        success: function(data){
            // get the output from database on success
            $("#id_suggesstions").show();//show the suggestions
            $("#id_suggesstions").html(data);//append data in the box for selection
            $("#searchword").css("background","#FFF");
        }
        });
    });
});
// call this function after select one of these suggestion for hide the suggestion box and select the value
function selectname(selected_value) {
	$("#searchword").val(selected_value);
	$("#id_suggesstions").hide();
}
</script>

create new readname.php for fetch data from send response and database usaing jQuery Autocomplete Search using PHP, MySQL and Ajax

<?php
// readname.php
// connect dbconnection file
require_once("dbconfig.php");
// hear we search term exist then process the below lines of code
if(!empty($_POST["searchterm"])) 
{
    // the query responsible for fetch matched data
    $sql_query ="SELECT * FROM username WHERE name like '" . $_POST["searchterm"] . "%' ORDER BY name LIMIT 0,4";
    $get_result = mysqli_query($dbconfig,$sql_query);
 
        if(!empty($get_result)) {
            // prepare the list for append
        ?>
                <ul id="name-list">
                <?php
                while($name_val = mysqli_fetch_array($result,MYSQLI_ASSOC))
				{
                ?>
					<li onClick="selectname('<?php echo $name_val["name"]; ?>');"><?php echo $name_val["name"]; ?></li>
                <?php 
				} 
				?>
                </ul>
        <?php } 
} 
?>

And the preceding issue we want to append information to go looking result space. when populating the result user will select simply that they need to settle on for jQuery Autocomplete Search.

All the steps mentioned within the comment hope all of you get a transparent understanding of Ajax search example. therefore don’t miss clicker. to urge updated subscribe our rss feed. Thanks for observation. i believe it’ll useful the tyro in PHP. Share together with your friends.

If you wish any facilitate relating to its configuration please be happy to comment we tend to like to assist you.

View Demo

1 thought on “jQuery Autocomplete Search using PHP, MySQL and Ajax”

  1. The demo link isn’t functional. When I fixed it manually, it’s just a zip file. Can you please make the demo an actual on-line demo? thank you

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  58  =  59

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US