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

    I have made a search form for users who have extended fields set up using Bob Rays Class Extender. The search is done by selecting checkboxes.
    The form has a hidden input for each option which returns 0 if the box is not checked.
    If the box is checked then the value returned is: Service Provider

    This is returning the results that I want.

    However I would like the state of the checkboxes to persist whilst the results are displayed. The results are displayed on the same page as the original search form.

    I have created a snippet that looks to see if an input is checked and returns 'checked' if it is. However this falls foul of my hidden input which is always returning zero. This results in all the boxes being ticked after the search is submitted.

    So what I need is for my snippet to check the box if the value submitted is: Service Provider.
    (Or perhaps not: 0)

    Here is a link to the page in question: http://dev.andytough.modxcloud.com/

    Here is the snippet: persistCheckboxState
    if (isset($_POST[$checkboxname])) {
        return 'checked="checked"';
      }


    Here is a sample of the code used in the form
    <form class="ext-user-search-form" id="ext-user-search-form" method="post">
        <input type="hidden" name="submit-var" value="etaoinshrdlu"/>
    
    <div>        
        <input type="hidden" name="user_search_dogtA" value="0"/>        
        <input type="checkbox" id="user_search_dogtAid" name="user_search_dogtA" value="Service Provider" [[!persistCheckboxState? &checkboxname=`user_search_dogtA`]] />
        <label for="user_search_dogtA">[[%user_search_dogtA_caption? &namespace=`classextender` &topic=`default`]]<br/>
        </label>
    </div>
    
    <div>        
        <input type="hidden" name="user_search_dogtB" value="0"/>        
        <input type="checkbox" id="user_search_dogtBid" name="user_search_dogtB"  value="Service Provider" [[!persistCheckboxState? &checkboxname=`user_search_dogtB`]]/>
        <label for="user_search_dogtB">[[%user_search_dogtB_caption? &namespace=`classextender` &topic=`default`]]<br/>
        </label>
    </div>
    


    This may not be needed but here are is the amended part of Bobs original search snippet

    <?php
    
    
    $output = $modx->getChunk($formTpl);
    
    $pdogtA = $modx->getOption('user_search_dogtA', $_POST, '');
    $pdogtB = $modx->getOption('user_search_dogtB', $_POST, '');
    $pdogtC = $modx->getOption('user_search_dogtC', $_POST, '');
    $pdogtD = $modx->getOption('user_search_dogtD', $_POST, '');
    $pdogtE = $modx->getOption('user_search_dogtE', $_POST, '');
    $pdogtF = $modx->getOption('user_search_dogtF', $_POST, '');
    $pdogtG = $modx->getOption('user_search_dogtG', $_POST, '');
    $pdogtH = $modx->getOption('user_search_dogtH', $_POST, '');
    $pdogtI = $modx->getOption('user_search_dogtI', $_POST, '');
    $pdogtJ = $modx->getOption('user_search_dogtJ', $_POST, '');
    $pdogtK = $modx->getOption('user_search_dogtK', $_POST, '');
    
    
    $modx->setPlaceholder('user_search_dogtA', $pdogtA);
    $modx->setPlaceholder('user_search_dogtB', $pdogtB);
    $modx->setPlaceholder('user_search_dogtC', $pdogtC);
    $modx->setPlaceholder('user_search_dogtD', $pdogtD);
    $modx->setPlaceholder('user_search_dogtE', $pdogtE);
    $modx->setPlaceholder('user_search_dogtF', $pdogtF);
    $modx->setPlaceholder('user_search_dogtG', $pdogtG);
    $modx->setPlaceholder('user_search_dogtH', $pdogtH);
    $modx->setPlaceholder('user_search_dogtI', $pdogtI);
    $modx->setPlaceholder('user_search_dogtJ', $pdogtJ);
    $modx->setPlaceholder('user_search_dogtK', $pdogtK);
    
    
    $fields = array();
    
    if (isset($_POST['submit-var']) && $_POST['submit-var'] == 'etaoinshrdlu') {
    
            $fields['where'] = '{"dogtA:=":"' . $pdogtA . '","OR:dogtB:=":"' . $pdogtB . '","OR:dogtC:=":"' . $pdogtC . '","OR:dogtD:=":"' . $pdogtD . '","OR:dogtE:=":"' . $pdogtE . '","OR:dogtF:=":"' . $pdogtF . '","OR:dogtG:=":"' . $pdogtG . '","OR:dogtH:=":"' . $pdogtH . '","OR:dogtI:=":"' . $pdogtI . '","OR:dogtJ:=":"' . $pdogtJ . '","OR:dogtK:=":"' . $pdogtK . '"}';
    
    
        $results = $modx->runSnippet('GetExtUsers', $fields);
    
    }
    
    if (! empty ($results) ){
        $modx->SetPlaceholder('user_search.results_heading',
            $modx->lexicon('ce_user_search_results_heading'));
        $modx->setPlaceholder('user_search.results', $results);
    }
    return $output;


    Any help would be very gratefully received. I have tried numerous variations all of which have failed and thought it might be time to ask for assistance!

    Andy

    This question has been answered by BobRay. See the first response.

    [ed. note: andytough last edited this post 7 years, 10 months ago.]
      If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

      email: [email protected] | website: https://andytough.com
    • discuss.answer
      • 3749
      • 24,544 Posts
      Give this a try:


      if (isset($_POST[$checkboxname]) && $_POST[$checkboxname]) {
          return ' checked="checked" ';
        }


      If that doesn't work, you can put a placeholder where the snippet tag is and set that placeholder to ' checked="checked" ' in the part of the code that detects the checked boxes.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 38783
        • 571 Posts
        Hello Bob,

        Thank you. Your code example above has worked perfectly.

        When I first saw it I had thought that I had used it before and had no luck - but I had actually added some extra bits that had stopped it from working!

        I had done this....
        if (isset($_POST['$checkboxname']) && $_POST['$checkboxname'] == 'Service Provider') {
            return 'checked="checked"';
          }
        


        Hopefully my php will improve over time.

        Best wishes,

        Andy

          If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

          email: [email protected] | website: https://andytough.com
          • 3749
          • 24,544 Posts
          Every time I use checkboxes, it takes a couple of tries to get them right. wink

          They don't work like other inputs.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting