Filter with checkboxs

  • I've only started using perch in the last few days. I've been able to get the filter form to filter results with a select box, but I'd like the user to be able to select multiple checkbox options and get a mix of results, I'm having trouble figuring this out, I added some '[ ]' to the php, but I must be missing something, when I click the filter button on the form, nothing happens and all the results still show.


    Below is my filter form code:

    Code
    1. <perch:form id="filter" method="get">
    2. <div>
    3. <perch:label for="ministryName">Type</perch:label>
    4. <perch:input type="checkbox" id="ministryName1" value="English Classes"> English Classes
    5. <perch:input type="checkbox" id="ministryName2" value="Youth Group"> Youth Group
    6. <perch:input type="checkbox" id="ministryName3" value="Hope Kids"> Hope Kids
    7. </div>
    8. <div><perch:input type="submit" value="Filter"></div>
    9. </perch:form>


    Here is my php code:


    Any assistance would be greatly appreciated :burd1fly:

  • drewm

    Approved the thread.
  • You should be able to use the name attribute


    Code
    1. <perch:input type="checkbox" id="ministryName1" name="ministries[]" value="English Classes">


    and then use perch_get('ministries') in your page. It should return an array of the selected values.


    Don't use the [] syntax when reading the value, only when building the form fields.

  • Thanks heaps for your reply Drew,


    The URL is outputting multiple values (http://localhost/hope/ministri…iesName%5B%5D=Youth+Group)


    But there are %5B%5D getting added to the URL and I'm getting an error on the web page:

    Warning: rawurldecode() expects parameter 1 to be string, array given in /Library/WebServer/Documents/hope/perch/core/runtime/core.php on line 11


    The error is referencing this line of code:

    PHP
    1. if (isset($_GET[$var]) && $_GET[$var]!='') {
    2. return rawurldecode($_GET[$var]);
    3. }


    I know that the %5B and %5D is code for [ & ] so I tried to replace them in the URL, but nothing happened with the results.


    Here is my updated form code:

    HTML
    1. <perch:form id="filter" method="get">
    2. <div>
    3. <perch:label for="ministryName">Type</perch:label>
    4. <perch:input type="checkbox" id="ministryName1" name="ministriesName[]" value="English Classes"> English Classes
    5. <perch:input type="checkbox" id="ministryName2" name="ministriesName[]" value="Youth Group"> Youth Group
    6. <perch:input type="checkbox" id="ministryName3" name="ministriesName[]" value="Hope Kids"> Hope Kids
    7. </div>
    8. <div><perch:input type="submit" value="Filter"></div>
    9. </perch:form>


    And here is my updated filter code:


    Looking forward to hearing from you

  • In the current version of Perch, perch_get() does not handle arrays. You can use PHP's $_GET directly instead.


    Alternatively, you can use the Pipit app's pipit_get() function, which works similarly to perch_get() but can handle arrays: https://grabapipit.com/pipits/apps/pipit/docs/pipit-get


    PHP
    1. if(pipit_get('ministriesName')) {
    2. $filters[] = array(
    3. 'filter' => 'ministriesName',
    4. 'match' => 'in',
    5. 'value' => implode(',', pipit_get('ministriesName', true)),
    6. );
    7. }