We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8921
    • 55 Posts
    ModX: 2.2.4-pl, FormIt 2.1.1

    I have read everything and tried everything there is to do with having checkbox arrays remember if they're checked or not. I have spent countless hours on this and have given up. Hopefully one of you kind souls will be able to give me a hand.

    I have created the most basic form in the world without any email processing, no validation, basically as simple as you can make it and it still doesn't work. I have also tried it with the default form code found in the FormIt documentation and that didn't work either.

    Here is the FormIt call and form code I'm currently using. You can see that I'm experimenting with FormItRetriever to try and get checkbox states remembered.

    [[!FormIt?
    &store=`1`
    &hooks=`redirect`
    &redirectTo=`1`
    ]]
    
    [[!FormItRetriever]]
    
    <form action="[[~[[*id]]]]" method="post" class="form">
    <input type="hidden" name="test" value="" />
    <input type="checkbox" name="test[]" value="Value1" [[!+fi.test:FormItIsChecked=`Value1`]] /> Value1<br/>
    <input type="checkbox" name="test[]" value="Value2" [[!+fi.test:FormItIsChecked=`Value2`]] /> Value2<br/>
    <input type="submit" value="Submit" />
    </form>


    You can also view my test form here: http://dev.formulate.ca

    Essentially I need to accomplish the following: Have the form reload the page it's currently on and remember what checkboxes were previously checked.

    Please help! Thank you.
      • 12028
      • 152 Posts
      Try this:
      <input type="hidden" name="test[]" value="" />

      instead of:
      <input type="hidden" name="test" value="" />


      Had you seen the documentation here:
      http://rtfm.modx.com/display/ADDON/FormIt.Handling+Selects%2C+Checkboxes+and+Radios ?

      And maybe/maybe not this can help you:
      http://forums.modx.com/thread/47698/selecting-multiple-checkboxes-simoultaneously---formit#dis-post-275580
        - A small step for mankind, so why not take two...

        Working with web production, graphic design/workflow, photo and education - but are trying to get a life in between!
        • 8921
        • 55 Posts
        Thanks for the response Henrik. Unfortunately, yes I have already tried that and it didn't work. I've literally spent 10+ hours reading and trying everything to get this to work. I can't fathom why checkboxes are so difficult with Formit. Anyway, I have read all the documentation and have previously seen (and tried) your post in the second link you provided.

        Any other ideas? Surely I can't be the only person that needs checkbox arrays to stay checked.
          • 32316
          • 387 Posts
          This works:

          [[!FormIt?
          &store=`1`,
          &hooks=`redirect`,
          &redirectTo=`1`
          ]]
          [[!FormItRetriever?]]
           
          <form action="[[~[[*id]]]]" method="post" class="form">
          <input type="hidden" name="test" value="" />
          <input type="checkbox" name="test" value="Value1" [[!+fi.test:FormItIsChecked=`Value1`]] /> Value1
          <input type="checkbox" name="test" value="Value2" [[!+fi.test:FormItIsChecked=`Value2`]] /> Value2
          <input type="submit" value="Submit" />
          
          </form>


          what I changed:
          missing ',' in the formit call
          changed test[] to test (as Henrik suggested)

          Note if you check both boxes only the second will be checked after submitting - if you have multiple checkboxes with the same 'name' then the last one in the code checked is the one that is used.

            • 32316
            • 387 Posts
            Here is some code with both check boxes working:

            [[!FormIt?
            &store=`1`,
            &hooks=`redirect`,
            &redirectTo=`[[*id]]`
            ]]
            [[!FormItRetriever?]]
             
            <form action="[[~[[*id]]]]" method="post" class="form">
            <input type="hidden" name="test" value="" />
            <input type="hidden" name="test2" value="" />
            
            <input type="checkbox" name="test" value="Value1" [[!+fi.test:FormItIsChecked=`Value1`]] /> Value1
            <input type="checkbox" name="test2" value="Value2" [[!+fi.test2:FormItIsChecked=`Value2`]] /> Value2
            <input type="submit" value="Submit" />
            
            </form>
              • Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 12028
                • 152 Posts
                Susan is right: The content of checkboxes and radios must have the ability to be an array.

                But it does not seem that FormItRetriever can handle arrays - and that's where the real problem occurs!
                  - A small step for mankind, so why not take two...

                  Working with web production, graphic design/workflow, photo and education - but are trying to get a life in between!
                • Hm. The array is passed, the snippet just doesn't break it down.

                  Array
                  (
                      ...
                  
                      [colors] => Array
                          (
                              [0] => blue
                          )
                  
                      [nospam] => 
                  )
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                  • The rtfm says
                    Use toPlaceholders() when working with multi-dimensional arrays or objects with variables other than scalars so each level gets delimited by a separator.
                    but I can't find any examples or explanation of how it would work.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 32316
                      • 387 Posts
                      using test[] and the color check boxes from the rtfm example:
                      - the data is passed to the new page (checked with a snippet that took the $_POST array and parsed it)
                      - the data is also saved in a cache file, mine was in - core/cache/default/web/elements/formit/submission/60e5dd_blah_etc_blah.cache.php

                      the cache contents were:
                      <?php if(time() > 1345401492){return null;} return array (
                        'test' => 
                        array (
                          0 => 'Value1',
                          1 => 'Value2',
                        ),
                        'color' => 
                        array (
                          0 => 'blue',
                          1 => 'red',
                          2 => 'green',
                        ),
                      );


                      It seems to me though that this will return 'null' and not the array of values (I'm assuming that 1345401492 is the time stamp of when the cache was created).

                      Edit: Of course it would be wrong to assume this!!! The time in the cache is 300 seconds bigger than the time of the creation of the page. So the array of data should be returned.

                      I have other things I need to attend to so that is much trouble shooting as I can do right now. [ed. note: whistlemaker last edited this post 11 years, 9 months ago.]