Autocomplete Search bar CODE Using jQuery, PHP, Ajax, SQL:

 FrontEnd



<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

</head>

<body>

<h2>Autocomplete Search bar using jQuery Ajax PHP and Mysql:</h2>

<label>Search</label>

<input type="text" name="find" id="find" placeholder="Find Name's"><br><br>

<div id="findList"></div>

</body>

</html>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<script>

$(document).ready(function(){

$('#find').keyup(function(){

var query = $(this).val();

if (query != '') {

$.ajax({

data : {query: query},

type : "post",

url : "search.php",

success: function(data) {

$('#findList').fadeIn();

$('#findList').html(data);

}

});

}else{

$('#findList').fadeOut();

$('#findList').html("");

}

});

$(document).on('click','li', function(){

$('#find').val($(this).text());

$('#findList').fadeOut();

});

});

</script>



BackEnd


<?php 

$conn = mysqli_connect('localhost','root','','text1') or die ('connection failed');

if (isset($_POST['query'])) {

$output = '';

$query = mysqli_query($conn, "select * from dbemployeelist where dbname like '%".$_POST['query']."%'");

$output = '<ul>';

if (mysqli_num_rows($query) > 0) {

while ($row = mysqli_fetch_array($query)) {

$output .='<li>'.$row["dbname"].'</li>';

}

}

else{

$output .= "<li>This Name not found</li>";

}

$output .='</ul>';

echo $output;

}

?>


Learning Video (YouTube):

Autocomplete Search bar


Screen Shot:

FrontEnd File

BackEnd File



Comments

Popular posts from this blog

C.R.U.D Oparetion in (PHP Code)

Search Dropdown - jQuery

JavaScript Form Validation Code