Auto Complete Searching

Auto Complete

Php code for auto complete ————–


$product_name_string = array();

	$query_result 	= 	$db->query("SELECT * FROM products ORDER BY name");
	if($db->numRows($query_result)) {	
		while($row = $db->next_record($query_result)){
			//$product_name_string .='"'.trim($row['name']).'--'.$row['product_id'].'", ';
			array_push($product_name_string,array('label'=>$row['name'],'id'=>$row['product_id']));
		}
		$_SESSION['product_name_string'] = $product_name_string;
	}

Jquery code for auto complete ——————


$(document).ready(function(){
	////////////////// Searching Medicine ///////////////////////////////////////////
    var availableProducts = <? echo json_encode($_SESSION['product_name_string']);?>;	
    $(".search_text").live("focus", function() {
		$(".search_text").autocomplete({
			source: availableProducts,
			delay: 0,
			minLength:1,
			select: function (event, ui) { 
				productId = ui.item.id;				
				$("#product_id").val(productId);
			}			
		});	
	});

Leave a Reply

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