How to create auto complete search in magento ?
// Use type ahead search for fast search results
// Put below mentioned library in your head section
<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/typeahead.js/0.9.3/typeahead.min.js"></script>-->
// Use below mention code in you catalogsearch/results.mini.phtml file
<script>
var gp = $.noConflict();
gp(document).ready(function(){
gp("#search").typeahead({
name : 'username',
remote: {
url : 'http://zapreaction.info/projects/vendors/zaplogin/search?query=%QUERY'
}
});
});
</script>
<form id="search_mini_form" action="<?php echo $catalogSearchHelper->getResultUrl() ?>" method="get">
<div class="search-box">
<label for="search"><?php echo $this->__('Categories:') ?></label>
<input id="search" type="text" name="<?php echo $catalogSearchHelper->getQueryParamName() ?>" value="<?php echo $catalogSearchHelper->getEscapedQueryText() ?>" class="input-text serch" maxlength="<?php echo $catalogSearchHelper->getMaxQueryLength();?>" placeholder="Search entire store here" />
<button type="submit" title="<?php echo $this->__('Search') ?>" class="button search-icon"><span><span><?php //echo $this->__('Search') ?></span></span></button>
<div id="search_autocomplete" class="search-autocomplete"></div>
<script type="text/javascript">
//<![CDATA[
var searchForm = new Varien.searchForm('search_mini_form', 'search', '<?php echo $this->__('Search entire store here...') ?>');
searchForm.initAutocomplete('<?php echo $catalogSearchHelper->getSuggestUrl() ?>', 'search_autocomplete');
//]]>
</script>
</div>
<div class="myrspns"></div>
</form>
<?php
// You can create you module and put below code on your module controller's file
class Zap_LoginWorld_SearchController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
$searchData = $_GET['query'];
require_once 'app/Mage.php';
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect(array('name', 'price'));
$collection->addAttributeToFilter('name', array('like' => '% '.$searchData.'%'));
foreach ($collection as $product) {
$res[] = $product->getName();
}
echo json_encode($res);
}
}
?>