We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45118
    • 123 Posts
    I have three radio buttons to sort items on a page, like this:
    <form action="[[~[[*id]]]]" method="POST">
       <input type="radio" name="sort_method" id="itemno" value="itemno" onChange="this.form.submit();" />Sort by Name
       <input type="radio" name="sort_method" id="height" value="height" onChange="this.form.submit();" />Sort by Height
       <input type="radio" name="sort_method" id="width" value="width" onChange="this.form.submit();;" />Sort by Width
    </form>


    The sorting works perfectly, but the radio button doesn't stay checked. I would like it to show which sorting method is active.
    (The form code is followed by a snippet that takes care of getting the items out of the database, sorting them and putting them on the page.)

    Thanks for your help!

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      Do you mean you want it to stay checked after the form is submitted, or stay checked for the next vistor to the page (or both)?

      Here's one way to do the first one:

      <form action="[[~[[*id]]]]" method="POST">
         <input type="radio" name="sort_method" [[+itemno_checked]] id="itemno" value="itemno" onChange="this.form.submit();" />Sort by Name
         <input type="radio" name="sort_method" [[+height_checked]] id="height" value="height" onChange="this.form.submit();" />Sort by Height
         <input type="radio" name="sort_method" [[+width_checked]] id="width" value="width" onChange="this.form.submit();;" />Sort by Width
      </form>


      In the part of the code that processes the form on submission, do this:

      $checked = 'checked="checked"';
      // Assuming that $val is the submitted value of the checkbox
      $modx->setPlaceholder($val . '_checked', $checked);


      Because the other placeholders aren't set, they'll be stripped out.

      To make things sticky between visits, you just do something similar when creating the form and just save the value somewhere to be use on the next visit. The value be saved in a TV attached to the form page, or probably better, as one of the snippet properties of the snippet that does the processing.

      There's an example here of doing that with checkboxes. http://bobsguides.com/blog.html/2013/08/18/making-form-checkboxes-sticky/

      A set of radio buttons would be simpler.


        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
        • 45118
        • 123 Posts
        Thank You Bob!!! This is wonderful.

        Yes, the first option, I want the radio to stay checked so I can see which sort method is active.

        This is how I made it work. Here's my original code to processes the form:
        $sortby = 'itemno';
        if (isset($_POST['sort_method']) && (! empty($_POST['sort_method']))) {
            $sortby = $_POST['sort_method'];
        }


        I combined it with your code, only replaced $val with $sortby, like this:
        $sortby = 'itemno';
        $checked = 'checked="checked"';
        if (isset($_POST['sort_method']) && (! empty($_POST['sort_method']))) {
            $sortby = $_POST['sort_method'];
        }
        $modx->setPlaceholder($sortby . '_checked', $checked);


        It works great! Thanks so much.
        • discuss.answer
          • 3749
          • 24,544 Posts
          Looks great. Glad I could help. smiley
            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