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

    Sorry for the mass of questions today. I have some javascript that dynamically adds forms to a page when you double click and I would like to capture all of the data and send it to a template variable related to the resource they were created on.

    I'm a little confused though how I would get formit to register the values if they are dynamically added. I assume I would use a hook after submitting and use the setTVvalue method? I've tried creating a hook with a static value first of all to see if the hook would update the TV and most of the time it doesn't work - I've tried clearing cache manually and in the manager.

    The hook/snippet I have is this:
    <?php
    $id_resource = $modx->resource->get('id');
    $id_tv = 46;
    $value = 'Updated Value';
    
    $tv = $modx->getObject('modResource', $id_resource);
    $tv->setTVValue('$id_tv', 'This is my hook value');
    $tv->save();


    So essentially two questions.

    1) What is wrong with my above hook/snippet?

    2) How do I capture dynamic form values to pass them into the snippet and ultimately save them to the TV?

    Thank you.
      • 3749
      • 24,544 Posts
      Remove the quotes around '$id_tv' wink -- Don't feel bad, I've made that mistake many times.

      FWIW, $tv might not be the best name for a variable that holds a resource Object.


        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
        • 54063
        • 14 Posts
        Ooops! Thanks. I'm still having the same issue though where its not updating. I just did a bit of reading though and think I need to write my snippet a little differently for hooks so i'll have a play smiley

          • 3749
          • 24,544 Posts
          Check the return value on the call to getObject:

          $tv = $modx->getObject('modResource', $id_resource);
          
          if (empty $tv)) {
             /* Report error */
          } else {
             $tv->setTVValue('$id_tv', 'This is my hook value');
          }
            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