<![CDATA[ FormIt Update Form Element on Form Submit - My Forums]]> https://forums.modx.com/thread/?thread=98818 <![CDATA[FormIt Update Form Element on Form Submit]]> https://forums.modx.com/thread/98818/formit-update-form-element-on-form-submit#dis-post-534339 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]]>
janwidmer Nov 13, 2015, 06:54 AM https://forums.modx.com/thread/98818/formit-update-form-element-on-form-submit#dis-post-534339
<![CDATA[Re: FormIt Update Form Element on Form Submit]]> https://forums.modx.com/thread/98818/formit-update-form-element-on-form-submit#dis-post-534548 ]]> BobRay Nov 18, 2015, 04:01 PM https://forums.modx.com/thread/98818/formit-update-form-element-on-form-submit#dis-post-534548 <![CDATA[Re: FormIt Update Form Element on Form Submit]]> https://forums.modx.com/thread/98818/formit-update-form-element-on-form-submit#dis-post-534525
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.');

...
]]>
janwidmer Nov 18, 2015, 06:59 AM https://forums.modx.com/thread/98818/formit-update-form-element-on-form-submit#dis-post-534525
<![CDATA[Re: FormIt Update Form Element on Form Submit (Best Answer)]]> https://forums.modx.com/thread/98818/formit-update-form-element-on-form-submit#dis-post-534413

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

$modx->setPlaceholder('total', $value1 + $value2);

]]>
BobRay Nov 14, 2015, 02:32 PM https://forums.modx.com/thread/98818/formit-update-form-element-on-form-submit#dis-post-534413
<![CDATA[Re: FormIt Update Form Element on Form Submit]]> https://forums.modx.com/thread/98818/formit-update-form-element-on-form-submit#dis-post-534397
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?]]>
janwidmer Nov 14, 2015, 03:37 AM https://forums.modx.com/thread/98818/formit-update-form-element-on-form-submit#dis-post-534397
<![CDATA[Re: FormIt Update Form Element on Form Submit]]> https://forums.modx.com/thread/98818/formit-update-form-element-on-form-submit#dis-post-534368
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.]]>
BobRay Nov 13, 2015, 01:20 PM https://forums.modx.com/thread/98818/formit-update-form-element-on-form-submit#dis-post-534368