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

    I'm trying to iterate through a table of checkboxes using a formit hook to update each selected resources's published status (or possibly other fields later)

    I'm having an issue with the below code where instead up getting each id and updating, it is creating a new resource and setting the published to 0.

    I think I am possibly selecting the resource incorrectly but I can't seem to figure it out. Any ideas?

    <?php
    
    // is user authenticated
    $user = $modx->getAuthenticatedUser();
    if(!$user){
      return false;
    }
    // which button was clicked
    $key = $hook->getValue('formkey');
    
    // if hiding update each resource published status to 0
    if ($key === 'hide') {
    
    foreach($_POST['rowid'] as $row) {
        $row = $modx->getObject('modResource', $rowid);
        $update = $row->get('published');
        $update->set('published', '0');
        $update->save();
        }
      
    }
    
    return;
    



    Using modx version 2.2.10 within modxcloud
      • 21414
      • 41 Posts
      I've updated my code to the following and it's no longer creating new resources. However it is still not updating the current ones. No errors in the logs.


      <?php
      
      // is user authenticated
      $user = $modx->getAuthenticatedUser();
      if(!$user){
        return false;
      }
      // which button was clicked
      $key = $hook->getValue('formkey');
      
      // if hiding update each resource published status to 0
      if ($key === 'hide') {
      if(isset($_POST['rowid'])){
      
          foreach($_POST['rowid'] as $row) {
              $update = $modx->getObject('modResource', $row);
              $update->get('published');
              $update->set('published', '0');
              $update->save();
          }
        
      }
      }
      
      
      
        • 45235
        • 6 Posts
        How is the $_POST['rowid'] variable setup? It is an array of some sort? Or is $row giving you an integer? If so try something like this:
        $update = $modx->getObject('modResource', array('id'=>$row));
          • 21414
          • 41 Posts

          I have a form that is set up more or less like this:

          [[!FormIt &hook=`snippet`]]
          
          <form  >
          
          <input type="hidden" name="formkey" value="hide">
          <!-- list of checkboxes -->
          <input type="checkbox" name="formid" value="98">
          <input type="checkbox" name="formid" value="99">
          <input type="checkbox" name="formid" value="100">
          <input type="checkbox" name="formid" value="101">
          
          <input type="submit" />
          </form>
          
          



          With the snippet that looks like this.

          <?php
          
          // is user authenticated
          $user = $modx->getAuthenticatedUser();
          if(!$user){
            return false;
          }
          // which button was clicked
          $key = $hook->getValue('formkey');
          
          // if hiding update each resource published status to 0
          if ($key === 'hide') {
          if(isset($_POST['rowid'])){
          
              foreach($_POST['rowid'] as $row) {
                 $update = $modx->getObject('modResource', array('id'=>$row));
                  $output = $update->get('published');
                  $output->set('published', '0');
                  $output->save();
                  return;
              }
          
          }
          }
          
          



          What i'm wanting it to do is get the array of id's that are selected within the form, and loop through each finding resource and updating it's published status to 0.


          I think i'm getting further off the mark instead of closer...
            • 45235
            • 6 Posts
            Seeing the HTML was helpful as I think your PHP snippet code should work okay. The fix is to name your checkboxes in the HTML so they are passed as an array:

            <input type="checkbox" name="formid[]" value="98">


            What was probably happening with your form is that FormIt was only passing the value of the last checkbox to your snippet.
              • 21414
              • 41 Posts
              Ahh yes I did forget the brackets! The snippet still does not seem to be processing though...

              Going to play with it some more this evening.
                • 21414
                • 41 Posts
                Figured it out... My form was getting escaped before the input elements so it wasn't passing them to my script...


                Also the script that ended up working is:

                <?php
                
                // is user authenticated
                $user = $modx->getAuthenticatedUser();
                if(!$user){
                  return false;
                }
                // which button was clicked
                $key = $hook->getValue('formkey');
                
                // if hiding update each resource published status to 0
                if ($key === 'hide') {
                if(isset($_POST['rowid'])){
                
                    foreach($_POST['rowid'] as $row) {
                       $update = $modx->getObject('modResource', array('id'=>$row));
                        $update->set('published', '0');
                        $update->save();
                        return;
                    }
                
                }
                }