How to create an AJAX search engine in PHP

Home > Search > How-to
  by

On the page which has the search engine, ensure onkeyup="autocomplet()" is in the input form.

<form>
    <input type="text" is="search_id" onkeyup="autocomplet()">
</form>

This is the JavaScript.

function autocomplet() {
	var min_length = 0; // min caracters to display the autocomplete
	var keyword = $('#search_id').val();
	if (keyword.length >= min_length) {
		$.ajax({
			url: 'ajax_search_processing.php',
			type: 'POST',
			data: {keyword:keyword},
			success:function(data){
				$('#search_results').show();
				$('#search_results').html(data);
			}
		});
	} else {
		$('#search_results').hide();
	}
}

function set_item(item) {
	// change input value
	$('#search_id').val(item);
	// hide proposition list
	$('#search_results').hide();
}

This is the SQL stuff.

<?php
$con = new mysqli('ip_domain','username','password','database');
$keyword = $POST['keyword'];
$sql = "select * from tablename where name like '%$keyword%' ";
$sql_query = mysqli_query($con,$sql);

while ($row = mysqli_fetch_array($sql_query)) {
echo $row['data'];
}
?>
<div id="search_results"></div>

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments