We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • So I'm making a custom snippet to make a page so my client can edit the context settings I put on his site, I know there's a plugin called ClientConfig by the awesome Mark Hamstra but the issue is I don't give my clients access to the manager they edit everything on the front end with NewsPublisher.

    With that said I already got the context settings rendered on the page and the form submits the changes (Thanks Mark Hamstra) but it is not saving the changes, any help would be greatly appreciated, thanks.

    Updated Code to reflect changes made

    <?php
    print '<form action="'.$target.'" method="POST">';
    
    if (isset($_POST['submit'])) {
        
        print "Form was successfully submitted.<br/>";
        
      $relatedSettings = $modx->getCollection('modContextSetting', array('area'=>$category));
      
      foreach ($relatedSettings as $Setting) {
          
        if (isset($_POST[$Setting->get('key')])) {
            
          $Setting->set('value', $_POST[$Setting->get('key')]);
          
          $Setting->save();
          
        //clear cache in MODx 2.1.x
        
        $cacheRefreshOptions =  array( 'system_settings' => array() );
        
        $modx->cacheManager-> refresh($cacheRefreshOptions);
        
        };
        
        $modx->log(modX::LOG_LEVEL_ERROR, '[Show Settings] - set value:'.$_POST[$Setting->get('key')]);
        
        $modx->log(modX::LOG_LEVEL_ERROR, '[My-Snippet] - POST:'.print_r($_POST, true) );
                
      };
    
    }
    
    if (!isset($relatedSettings)) $relatedSettings = $modx->getCollection('modContextSetting', array('area'=>$category));
    
    foreach ( $relatedSettings as $Setting ) {
        
        print '<label>'.$Setting->get('key').'</label><br/><input type="text" name="'.$Setting->get('key').'" value="'.$Setting->get('value').'">';
    
        };
    
    print "<input type='submit' value='Save' class='alignright' name='submit'><div class='clearfix'></div>";
    
    print '</form>';
    


    What I get in the error logs is the following:

    [2013-01-15 06:06:10] (ERROR @ /index.php) [Show Settings] - set value:
    [2013-01-15 06:06:10] (ERROR @ /index.php) [My-Snippet] - POST:Array
    (
        [submit] => Save
    )
    


    I get one of these for each setting on the page. [ed. note: benmarte last edited this post 11 years, 4 months ago.]
      Benjamin Marte
      Interactive Media Developer
      Follow Me on Twitter | Visit my site | Learn MODX
    • Have you tried putting in some error trapping in your loop:

      $modx->log(modX::LOG_LEVEL_ERROR, '[My-Snippet] - set value:'.$_POST[$Setting->get('key')]);
      


      Or maybe you need to use this method for instant change: http://rtfm.modx.com/display/xPDO20/setOption
      $modx->setOption($key, $newvalue);
      
      • @jgulledge The error log is returning blank values so it's not passing the values on submission of the form so thanks for that suggestion.

        I tried changing line 14 to this;
        $Setting->setOption('value', $_POST[$Setting->get('key')]);


        That didn't work either, thanks for checking though much appreciated.
          Benjamin Marte
          Interactive Media Developer
          Follow Me on Twitter | Visit my site | Learn MODX
        • Try putting this in to see what everything that is in your post:

          $modx->log(modX::LOG_LEVEL_ERROR, '[My-Snippet] - POST:'.print_r($_POST, TRUE) );
          
          
            • 3749
            • 24,544 Posts
            Ben, try setting the allow_tags_in_post System Setting to true. It's off by default as of MODX 2.2.6. You'll get a warning in the Dashboard, but I think it may solve your problem.

            Another user tried using setOption('allow_tags_in_post', true) in the NP code but it didn't work for them. I can't think of any other easy way to solve it.

              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
            • @Bob That settings is set to true so it must be something else, again this is a custom page on the front end not a CMP on the backend just wanted to make that clear not sure if that might be the issue.

              Thanks again for checking guys.
                Benjamin Marte
                Interactive Media Developer
                Follow Me on Twitter | Visit my site | Learn MODX
                • 3749
                • 24,544 Posts
                Ah, I see what you're doing, but I don't see any reason why it shouldn't work unless the $_POST values aren't really set on submission or are misnamed.
                  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
                • @jgulledge so I inserted the print_r and now I get this as an error repeated for each setting

                   [2013-01-14 20:34:16] (ERROR @ /index.php) [My-Snippet] - POST:Array
                  (
                      [submit] => Save
                  )
                  
                  


                  Any ideas what this means? I was expecting to get back the value of each input field, pardon my PHP n00bness, thanks again guys.
                    Benjamin Marte
                    Interactive Media Developer
                    Follow Me on Twitter | Visit my site | Learn MODX
                  • Shot in the dark (haven't had a moment to try out your code) but the two things that spring to mind:

                    • Are web-users allowed to change system settings? (do they need mgr session/permissions)
                    • I've had some issues getting POST/GET params via snippets (somewhat sporadically, which is irritating). There is an array of request params held somewhere in $modx->request->[something] i think, that may help you get straight to the data
                    • This:
                      print '<label>'.$Setting->get('key').'</label><br/><input type="text" value="'.$Setting->get('value').'">';

                      should be
                      print '<label>'.$Setting->get('key').'</label><br/><input type="text" name="'.$Setting->get('key').'" value="'.$Setting->get('value').'">';

                        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.