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

    I am trying to develop a form for editing a database record and it has a form which features an array of checkboxes as seen below...

    <tr>
    <td>Cell Codecs:</td>
    <td>
        <input type="checkbox" name="cellcodecs[]" id="cell_g729" value="G729" [[!+fi.cellcodecs:FormItIsChecked=`G729`]]> G729<br>
        <input type="checkbox" name="cellcodecs[]" id="cell_g711a" value="G711a" [[!+fi.cellcodecs:FormItIsChecked=`G711a`]]> G711a<br>
        <input type="checkbox" name="cellcodecs[]" id="cell_g711u" value="G711u" [[!+fi.cellcodecs:FormItIsChecked=`G711u`]]> G711u<br>
        <input type="checkbox" name="cellcodecs[]" id="cell_gsm" value="GSM" [[!+fi.cellcodecs:FormItIsChecked=`GSM`]]> GSM<br>
        <input type="checkbox" name="cellcodecs[]" id="cell_ilbc" value="iLBC" [[!+fi.cellcodecs:FormItIsChecked=`iLBC`]]> iLBC
    </td>
    </tr>


    Now being that these are checkboxes, any number of them can be selected. If I use $hook->setValue('cellcodecs', 'G729'); I could set the first checkbox as being checked. How do I go about accessing and checking the other checkboxes based on their values? I have tried things like $hook->setValue('cellcodecs', array(0 => 'G729', 1 => 'G711a')); but this only checks the first box again. Anyone have some function for helping select multiple checkboxes setup in an array like this?

    Thanks!

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

      • 3749
      • 24,544 Posts
      I would forget about FormIt and just use placeholders like this:

      td>
      <input type="checkbox" name="cellcodecs[]" id="cell_g729" value="G729" [[+G729-checked`]]> G729

      <input type="checkbox" name="cellcodecs[]" id="cell_g711a" value="G711a" [[+G711a-checked`]]> G711a

      <input type="checkbox" name="cellcodecs[]" id="cell_g711u" value="G711u" [[+G711u-checked`]]> G711u

      </td>

      Then something like this in the preHook:

      $modx->setPlaceholder('G729-checked', 'checked="checked"');
      $modx->setPlaceholder('G711a-checked', 'checked="checked"');
      $modx->setPlaceholder('G711u-checked', '');
        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
      • discuss.answer
        • 13428 ☆ A M B ☆
        • 1,031 Posts
        Should work this way:

        $hook->setValue('cellcodecs', json_encode(array('G729', 'G729')));

          • 49479
          • 2 Posts
          Thanks to both of you. I have tried both ways and both work. Jako's answer is nice and clean and works really well. I think they should actually add that tidbit of info to the FormIt documentation ModX page because it had nothing about setting values in such a situation and this type of thing would happen often. smiley
            • 13428 ☆ A M B ☆
            • 1,031 Posts
            In my opinion FormIt should prepare a placeholder 'cellcodes' with the JSON encoded array. Have to look if that could be patched in without removing the deeper nested placeholders.