On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hi,
    I am creating a form, where people can spend money for wishlist items. (For a wishlist of a wedding page). Every Item has a total amount, a current amount and a remaining amount being stored as TVs.

    In the form to submit to give money, there is a field for the amount, the person wants to give. Next to that field, the remaining amount of this wishlist item is being displayed. (taken from the TV of that resource)

    See Attachment for Printscreen of the form

    When the user submits the form, the remaining Amount should be updated by adding the amount of the user to the current amount of that wishlist item. I got that all up and working by storing the values as template variables of the wishlist item page and using a custom hook to update the tv values accordingly.

    My only problem is, now when the user submits the form and the page reloads, the remaining amount next to the amount field is still the same as before. Only after making a regular page reload, the remaining amount gets updated.

    I tried several things to make it work:

    - Calling everything uncached
    - Using a uncached Snippet directly in the form template, which reads the remaining amount directly from the resource's template variable
    - using FormItRetriever and my custom Hook to set the new remaining value directly in the hook
    Nothing seems to work. It seems like, that the form fields are not parsed again on form submit, but just displayed again

    A workaround would be to just hide the form fields on submit and only show the thank you message, but I also cannot find any input, if that's possible

    Any Idea?

    Best
    Jan

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

    • Is your FormIt tag above the form on the page?

      You might try making the placeholder tag for that amount uncached:

      [[!+placeholderName]]


      If that doesn't work, you might try a preHook that sets that placeholder from the $_POST values.
        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
      • Hi Bob,

        Yes, the formIt Tag is above the Form. I also tried to call the placeHolder for the remaining amount uncached, but that did not seem to change anything.

        How would I set the placeholder in a preHook? (How to set a preHook is clear), but would I set it directly in the $_POST Variable?
        • discuss.answer
          Something like this:


          $value1 = $modx->getOption('fieldName1', $_POST);
          $value2 = $modx->getOption('fieldName2', $_POST);
          
          $modx->setPlaceholder('total', $value1 + $value2);

            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
          • Hi All,

            I made it working, thanks for your help.

            My original Problem was, that the remainingAmount was not a form element (input field), but only a Span containing text. This span was not getting updated onFormSubmit (Now that I think about it, it makes kinda sense..)

            No I changed it to be an readonly Input field. By Using a Prehook, I am getting the remainingAmount from my Resource TV.
            In my Custom Form Hook, I am updating the form Placeholder with the new amount, so that it is updated after the form submit.

            Updating the form placeholder also after submitting the form is needed, because at the time, the preHook gets executed, the system does not know the new amount yet.

            Short Code Snippets:

            formIt preHook to get remainingAmount
            <?php
            
            $pageId = $modx->resource->get('id');
            $page = $modx->getObject('modResource', $pageId);
            
            $remainingAmount = $page->getTVValue('remainingAmount');
            
            $hook->setValue('remainingAmount', ' / CHF ' . $remainingAmount);
            
            return true;
            


            Readonly Form Field to display the amount
            <input type="text" readonly="readonly" class="remaining-amount" value="[[!+fi.remainingAmount]]">
            


            Custom hook to update TV's and update Placeholder
            ...
            
            $modx->setPlaceholders(array(
               'remainingAmount' => ' / CHF ' . $newRemainingAmount,
            ),'fi.');
            
            ...
            
            • I'm glad you got it sorted. Thanks for posting your solution. smiley
                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