We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22574
    • 181 Posts
    Hi Guys,

    I am trying to make a custom search form. I tired ditto with filters but capitals and exact matches have lead me to thinking that its not a good choice.

    So far I have a form with a field for job title which I would like to search all the page titles, a location field which should search all TVs with the same name and lastly a general keyword field which searches everything including content.

    [!AjaxSearch? &landingPage=`20` &parents=`6,7` &tplInput=`searchform`!]


    <form id="ajaxSearch_form"  action="index.php?q=search-results.html" method="post" style="width:530px;">
        <input type="hidden" name="advSearch" value="oneword" />
        <div style="float:left;">
     <label for="title">
          Job Title: 
    <br/><input id="title" class="cleardefault" type="text" name="title" style="width:240px;" />
        </label>
        </div>
    <div style="float:right;">
    <label for="location">
          Location:
    <br/><input id="location" class="cleardefault" type="text" name="location" style="width:240px;" />
        </label>
    </div>
    <div style="clear:both"></div>
        <p/><label for="ajaxSearch_input">
          Keyword:
    <br/><input id="ajaxSearch_input" class="cleardefault" type="text" name="search" style="width:240px;" />
        </label>
        <label for="ajaxSearch_submit">
          <p/><input id="ajaxSearch_submit" type="submit" name="sub" value="Search" class="searchbtn" />
        </label>
    </form>

      • 22574
      • 181 Posts
      Okay so figured it!

      I went back to ditto after seeing some other examples.
      This one is a bit different as the user inputs the fields so capitals and exact matches were creating problems.

      I call it like this:
      {{searchform}}
      [!dittofilter!]


      Form code - make your own TV's and change the code below:
      <form id="ajaxSearch_form"  action="index.php?q=search-results.html" method="post" style="width:530px;">
          <input type="hidden" name="advSearch" value="oneword" />
          <div style="float:left;">
       <label for="title">
            Job Title: 
      <br/><input id="title" class="cleardefault" type="text" name="title" style="width:240px;" />
          </label>
          </div>
      <div style="float:right;">
      <label for="location">
            Location:
      <br/><input id="location" class="cleardefault" type="text" name="location" style="width:240px;" />
          </label>
      </div>
      <div style="clear:both"></div>
          <p/><label for="ajaxSearch_input">
            Keyword:
      <br/><input id="ajaxSearch_input" class="cleardefault" type="text" name="search" style="width:240px;" />
          </label>
          <label for="ajaxSearch_submit">
            <p/><input id="ajaxSearch_submit" type="submit" name="sub" value="Search" class="searchbtn" />
          </label>
      </form>


      Custom ditto call:
      <?php
      global $modx;
      $filter = "";
      if (!empty($_POST['title'])) $filter .= (empty($filter)?"":"|")."pagetitle,".mysql_escape_string($_POST['title']).",9";
      if (!empty($_POST['location'])) $filter .= (empty($filter)?"":"|")."location,".mysql_escape_string($_POST['location']).",9";
      if (!empty($_POST['search'])) $filter .= (empty($filter)?"":"|")."content,".mysql_escape_string($_POST['search']).",9";
      return $modx->runSnippet('Ditto', array('parents'=>'6,7', 'tpl'=>'vacancies', 'display'=>'10', 'sortBy'=>'pub_date', 'phx'=>'-', 'paginate'=>'1', 'filter'=>$filter, 'noResults'=>'Sorry no results. Please try a broader search.'));
      ?>


      Make yourself a TPL for the results and that's it.
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        This is brilliant! It solves a complex search problem I've been trying to figure out for several days now.

        Four tabs, one "All" showing a categorized list of all results by a primary category and three other tabs, breaking down the search results into secondary categories.

        So I can do it easily with one AjaxSearch for the "All" tab results, and three Ditto calls for the secondary tabs! All I need to do is to figure out how to add the three runSnippet functions into the AjaxSearch processing.

        The three secondary results tabs are for items whose primary category will be clearly indicated by the page title, so just links using the page title will work fine, no extract necessary.

        A bit slow on the first run, but once the Ditto calls are cached it'll be fine.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org