We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11243
    • 27 Posts
    Hi, I am using AjaxSearch 1.8.4 at the moment and am trying to allow web users on my site to be able to select from one or a number of categories (relating to various MODx parent folders on the site) in which to search.

    I have currently set this up by using a number of check-boxes in the search form, each named "parents[]" with a value of the respective category (parent folder) ID. I have then tweaked the AjaxSearch snippet to dynamically write the selected check-box values into the &parents parameter at run-time.

        $catgs = $_POST["parents"]; 
        $how_many = count($catgs); 
        if ($how_many>0) { 
           $parents = implode(",",$catgs); 
        }
    
        $cfg['parents'] = isset($parents) ? $parents : (isset($__parents) ? $__parents : $dcfg['parents']);
    


    This part seems to work well, allowing a combination of different categories to be searched, but I am having trouble in trying to re-display which check-boxes were previously selected in subsequent searches. Does anybody have any ideas on how this can be achieved or a better method of choosing multiple folders in which to search as I am already nervous about changing the snippet code for future release upgrade problems?

    Thanks in advance...
      • 5811
      • 1,717 Posts
      Do you use the non-ajax mode ? If yes, the better way is probably to encapsulate the building of the checkboxes thru a snippet and call this snippet in your template. This template should display the checkbox in the form but also initialise the "checked" value if needing, depending the $_POST variable.


      Regarding this subject (Allowing user to define multiple categories in which to search), these three demos could interest you:

      1/ this demo, allow a search in several categories. Category is define by a TV. Document of the same category as the same tv value. The documents are not necessarly in the same container (hierarchy of documents).
      Here by default when not checkboxes are ticked, all the results are displayed

      Otherwise, look also at this demo. Here the checkboxes are ticked after, depending the search results.


      2/ this demo, allow a search in several subsite domains (english documents / french documents). By subsite domain, I mean here, different containers. Here by default, without a domain selected, no results are displayed.
        • 11243
        • 27 Posts
        Hi Coroico,

        Many thanks for your feedback on this one, and yes, I meant to say that I am in non-ajax mode.

        I like your suggestion of encapsulating the check-boxes in a snippet and evaluating the $_POST variable in there (why didn’t I think of that? huh ) so I’ll give that a try - looks like it should work well.

        Also noticed that your post-checking of checkboxes in the second demo proves that it can be done, which gives me hope. The category TV idea may be more flexible going forward, but there are a lot of docs I would have to post-tag to implement this method in the short term.

        Cheers again, will post back when I get this working...

        UPDATE...This works! smiley

        Here’s my snippet and the new chunk:

        Chunk code for checkbox display:
        <p><input type="checkbox" id="parents1" name="parents[]" value="52" [[isChecked? &boxno=`52` ]] /> <label for="parents1">Category 1</label></p>
        <p><input type="checkbox" id="parents2" name="parents[]" value="63" [[isChecked? &boxno=`63` ]] /> <label for="parents2">Category 2</label></p> 
        


        And the isChecked snippet:
        <?php
        if (isset($_POST["parents"])) {
           $catgs = $_POST["parents"]; 
            if (in_array($boxno,$catgs)) 
            { 
               $output = "CHECKED"; 
            } else {
               $output = "";
            }
        } else {
               $output = "CHECKED"; 
        }
            return $output;
        ?>


        Thanks for your help!