Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SS ajax site search
<?php
class Page_Controller extends ContentController
{
public function init()
{
parent::init();
Requirements::javascript('framework/thirdparty/jquery/jquery.js');
Requirements::javascript('themes/simple/javascript/script.js');
}
public function results($data, $form, $request)
{
$data = array(
'Results' => $form->getResults(),
'Query' => DBField::create_field('Text', $form->getSearchQuery()),
'Title' => _t('SearchForm.SearchResults', 'Search Results')
);
if ($this->getRequest()->isAjax()) {
return $this->customise($data)->renderWith('SearchResults');
}
return $this->owner->customise($data)->renderWith(array('Page_results', 'Page'));
}
}
jQuery.noConflict();
(function($) {
$(document).ready(function() {
//...
$('#SearchForm_SearchForm').on('submit', function(e) {
e.preventDefault();
const data = $(this).serialize();
const $form = $(this);
$.ajax({
url: $form.attr('action'),
type: 'GET',
data
})
.done(response => {
$('#results-target').html(response);
});
});
});
}(jQuery));
<% if $Results %>
<ul id="SearchResults">
<% loop $Results %>
<li>
<h4>
<a href="$Link">
<% if $MenuTitle %>
$MenuTitle
<% else %>
$Title
<% end_if %>
</a>
</h4>
<% if $Content %>
<p>$Content.LimitWordCountXML</p>
<% end_if %>
<a class="readMoreLink" href="$Link" title="Read more about &quot;{$Title}&quot;">Read more about &quot;{$Title}&quot;...</a>
</li>
<% end_loop %>
</ul>
<% else %>
<p>Sorry, your search query did not return any results.</p>
<% end_if %>
<!DOCTYPE html>
<!--
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Simple. by Sara (saratusar.com, @saratusar) for Innovatif - an awesome Slovenia-based digital agency (innovatif.com/en)
Change it, enhance it and most importantly enjoy it!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-->
<!--[if !IE]><!-->
<html lang="$ContentLocale">
<!--<![endif]-->
<!--[if IE 6 ]><html lang="$ContentLocale" class="ie ie6"><![endif]-->
<!--[if IE 7 ]><html lang="$ContentLocale" class="ie ie7"><![endif]-->
<!--[if IE 8 ]><html lang="$ContentLocale" class="ie ie8"><![endif]-->
<head>
<% base_tag %>
<title><% if $MetaTitle %>$MetaTitle<% else %>$Title<% end_if %> &raquo; $SiteConfig.Title</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
$MetaTags(false)
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<% require themedCSS('reset') %>
<% require themedCSS('typography') %>
<% require themedCSS('form') %>
<% require themedCSS('layout') %>
<link rel="shortcut icon" href="$ThemeDir/images/favicon.ico" />
</head>
<body class="$ClassName<% if not $Menu(2) %> no-sidebar<% end_if %>" <% if $i18nScriptDirection %>dir="$i18nScriptDirection"<% end_if %>>
<% include Header %>
<div class="main" role="main">
<div class="inner typography line">
$Layout
<div id="results-target"></div>
</div>
</div>
<% include Footer %>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment