Skip to content
  1. Extras
  2. mSearch
  3. Snippets
  4. mSearchForm

mSearchForm ​

Snippet for outputting the search form with autocomplete.

Uncached call

Call the snippet uncached (with !), as it registers scripts and uses the current query from $_REQUEST.

Parameters ​

ParameterDefaultDescription
tplmSearch.formChunk with search form
pageIdcurrent pagePage ID for search form action
autocomplete0Enable autocomplete (1 or 0)

Placeholders in tpl chunk ​

PlaceholderDescription
[[+query]]Current query (escaped)
[[+action]]Form action URL
[[+autocomplete]]true or false — autocomplete on/off

Examples ​

Basic call ​

fenom
{'!mSearchForm' | snippet}

With results page ​

fenom
{'!mSearchForm' | snippet: [
    'pageId' => 5,
    'autocomplete' => 1
]}

On separate page ​

Form on homepage, results on /search/ (ID = 5):

fenom
{'!mSearchForm' | snippet: [
    'pageId' => 5
]}

Form and results on same page ​

fenom
{'!mSearchForm' | snippet: [
    'pageId' => $_modx->resource.id,
    'autocomplete' => 1
]}

{'!mSearch' | snippet: [
    'tpl' => 'mSearch.row',
    'limit' => 10
]}

Default chunk ​

Chunk mSearch.form:

fenom
<form action="{$action}" method="get" class="msearch-form" data-autocomplete="{$autocomplete}">
    <input type="text" name="mse_query" value="{$query}" placeholder="{'mse_search_placeholder' | lexicon}" />
    <button type="submit">{'mse_search_button' | lexicon}</button>
</form>

Autocomplete ​

When autocomplete is enabled the snippet registers a JS file that adds suggestions while typing.

How it works ​

  1. User types in the query
  2. JS sends an AJAX request to the server
  3. Server looks up matches in query history
  4. User sees a list of suggestions

Styling ​

Scripts and styles are set in system settings:

SettingDefaultDescription
mse_frontend_css[[++assets_url]]components/msearch/css/web/msearch.cssCSS file
mse_frontend_js[[++assets_url]]components/msearch/js/web/msearch.jsJS file

To change styles or scripts, copy files elsewhere and set new paths in settings so updates do not overwrite them.

Customizing the form ​

Simple form without snippet ​

fenom
<form action="{'search' | url}" method="get">
    <input type="text" name="mse_query" value="{$_GET['mse_query']}" placeholder="Search..." />
    <button type="submit">Search</button>
</form>

Form with extra fields ​

fenom
<form action="{'search' | url}" method="get" class="msearch-form">
    <div class="search-row">
        <input type="text" name="mse_query" value="{$_GET['mse_query']}" placeholder="Search..." />
        <select name="category">
            <option value="">All categories</option>
            <option value="5">News</option>
            <option value="10">Articles</option>
        </select>
    </div>
    <button type="submit">Search</button>
</form>

Styled form ​

fenom
{'!mSearchForm' | snippet: [
    'tpl' => 'mySearchForm',
    'pageId' => 5,
    'autocomplete' => 1
]}

Chunk mySearchForm:

fenom
<form action="{$action}" method="get" class="search-widget" data-autocomplete="{$autocomplete}">
    <div class="search-input-wrapper">
        <i class="icon-search"></i>
        <input
            type="text"
            name="mse_query"
            value="{$query}"
            placeholder="What are you looking for?"
            autocomplete="off"
        />
        {if $query}
        <button type="button" class="clear-search" onclick="this.form.mse_query.value=''; this.form.submit();">
            <i class="icon-close"></i>
        </button>
        {/if}
    </div>
    <button type="submit" class="search-submit">
        <span>Search</span>
    </button>
</form>

Differences from mSearch2 ​

AspectmSearch2mSearch
Form chunk paramtplFormtpl
Autocompleteresults, queries, 01 or 0
Query variableConfigurable (queryVar)Fixed mse_query
Snippet as elementSupportedNot supported

Simpler architecture

mSearch autocomplete uses only query history. For more complex cases use AJAX with the mSearch snippet.