Ajax Autocomplete Search using jQuery with PHP MySQLi

Ajax Autocomplete Search using jQuery with PHP MySQLi

In this Post We Will Explain About is Ajax Autocomplete Search using jQuery with PHP MySQLi With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to jquery autocomplete multiple fields using jquery ajax php and mysqli Example

In this post we will show you Best way to implement Ajax Autocomplete textbox using jQuery, PHP and MySQLi, hear for Autocomplete search using jQuery Ajax with PHP MySQLi with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Also Read :   Angularjs Tutorial - Angular Example




Creating simple Search Form

Here, I Make simple query search form where I search for a querystr, Make a new HTML file, name Like as index.html and simple copy paste the source codes below.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Autocomplete search using PHP, MySQLi, Ajax and jQuery</title>
	<link rel="stylesheet" type="text/css" href="bootstrap4/css/bootstrap.min.css">
	<style type="text/css">
		.gst20{
			margin-top:20px;
		}
		#live_search{
			display: none;
		}
		.list-gpfrm-list a{
			text-decoration: none !important;
		}
		.list-gpfrm li{
			cursor: pointer;
		}
		.list-gpfrm li:hover{
			color: white;
			background-color: #3d3d3d;
		}
	</style>
</head>
<body>
<div class="container">
	<h1 class="text-center gst20">Auto Complete Search using jQuery</h1>
	<div class="row justify-content-center gst20">
		<div class="col-sm-6">
			<form id="liveForm" method="POST" action="">
				<div class="input-gpfrm input-gpfrm-lg">
				  	<input type="text" id="querystr" name="querystr" class="form-control" placeholder="UserName, SirName or Nickname" aria-describedby="basic-addon2">
				  	<div class="input-gpfrm-append">
				    	<button class="btn btn-outline-secondary" type="submit">Find or Search</button>
				  	</div>
				</div>
			</form>
			<ul class="list-gpfrm" id="live_search"></ul>
		</div>
	</div>
</div>
 
<script src="jquery.min.js"></script>
<script src="app.js"></script>
</body>
</html>

Creating simple jQuery Script

And then, I make simple jQuery script where I Add all simple main jquery functions and http request.create a new file, name Like as app.js and here copy paste the source codes below.

Also Read :   Autocomplete textbox using jQuery PHP and MySQL

$(document).ready(function(){
//Autocomplete search using PHP, MySQLi, Ajax and jQuery
	//generate suggestion on keyup
	$('#querystr').keyup(function(e){
		e.preventDefault();
		var form = $('#liveForm').serialize();
		$.ajax({
			type: 'POST',
			url: 'do_search.php',
			data: form,
			dataType: 'json',
			success: function(response){
				if(response.error){
					$('#live_search').hide();
				}
				else{
					$('#live_search').show().html(response.data);
				}
			}
		});
	});
 
	//fill the input
	$(document).on('click', '.list-gpfrm-list', function(e){
		e.preventDefault();
		$('#live_search').hide();
		var fullname = $(this).data('fullname');
		$('#querystr').val(fullname);
	});
});

Creating simple Search Script

Lastly, I make the script that find or searches simple database from the inputted querystr,Male a new file, name it as do_search.php and paste the codes below.

<?php
//Autocomplete search using jQuery Ajax with PHP MySQLi
	if(isset($_POST['querystr'])){
		//database connection
		$conn = new mysqli('localhost', 'root', 'user_pass', 'live24u');
 
		//simple initialize results
		$results = array('error' => false, 'data' => '');
 
		$querystr = $_POST['querystr'];
 
		//check if querystr is empty
		if(empty($querystr)){
			$results['error'] = true;
		}
		else{
			//fetching data from mysql database via querystr
			$sql = "SELECT * FROM users WHERE username LIKE '%$querystr%' OR sirname LIKE '%$querystr%' OR nickname LIKE '%$querystr%'";
			$sqlquery = $conn->query($sql);
 
			if($sqlquery->num_rows > 0){
				while($ldata = $sqlquery->fetch_assoc()){
					$results['data'] .= "
						<li class='list-gpfrm-list' data-fullname='".$ldata['username']." ".$ldata['sirname']."'>".$ldata['username']." ".$ldata['sirname']."</li>
					";
				}
			}
			else{
				$results['data'] = "
					<li class='list-gpfrm-list'>No found data matches Records</li>
				";
			}
		}
 
		echo json_encode($results);
	}
?>

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Also Read :   Jquery select2 ajax autocomplete using PHP/MySQLi





Example

I hope you have Got What is Autocomplete search using jQuery Ajax with PHP MySQLi And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.