Formit setting from input fields

Hello I had different forms and I’m trying to use only one [[!formit call for all of them.

[[!FormIt? 
    &preHooks=`formCustomFields`
    &hooks=`spam,email,redirect`
	&emailFrom=`[[+email]]`
	&emailFromName=`[[+firstname]] [[+lastname]]`
	&emailSubject=``
    &redirectTo=``
	&emailTo=`[[++email]]`
	&emailTpl=``
    &validate=`nospam:blank`
]]

So I create a form inside a chunk called [[$Form]] that include 3 hidden input fields with the settings I need to replace:

	<input type="hidden" name="subject" value="[[+subject]]" />
	<input type="hidden" name="redirect" value="[[+redirect]]" />
	<input type="hidden" name="tpl" value="[[+tpl]]" />

Then I fill the hidden fields with the right info, using the chunk call:

[[$contactForm? &subject='Form One' &redirect='53' &tpl='chunkFormOne']]

Now I create a &preHooks=‘formCustomFields’ where I 'm trying to read and set the settings:

<?php
$formit =& $hook->formit;
$values = $hook->getValues();

$subject = $values['subject'];
$redirect = $values['redirect'];
$tpl = $values['tpl'];

$formit->config['emailSubject'] = $subject;
$formit->config['redirectTo'] = $redirectTo;
$formit->config['emailTpl'] = $tpl;

return true;

But the Freaking thing is not working… what am I doing wrong?

any help will be welcome, Thanks!!!

Does it work if you remove the spam hook and &validate property?

No Bob, still not working… any other idea master?

What is not working? The sending part, the filling part? If it’s the sending part, does the &emailFrom part has a correct (domain related) emailaddress?

getValues() captures data in a hook (not a preHook) that has already been submitted using the form. If you’re using a preHook to set values I think you need to use the setValue method instead. However, if you’re passing the values in directly via the chunk call then I don’t think you need to do anything with hooks? Can you not put the placeholders directly in the formit call ie:

&emailSubmit=`[[+subject]]`

Hello mate.
If I fill everything as normal it works fine, so the &emailFrom or any other seeting are not the problem.
The problem is when I try to fill the [[!itform call dynamically it doesn’t send anything just refresh the form page, didn’t send or go to the thank you page.

Man that was my first try just filling the fields with placeholders it didn’t work.
My theory was that the placeholder for redirect is needed before the form is processed so I create the snippet and added in the hooks list between span and mail:

hooks='spam,formCustomFields,email,redirect'

but that didn’t work neither that is why I tried the &preHook because the settings are already there before the form is loaded, but nothing is happening.

HEEELLLP!!! haha. Please!

Try this hook

<?php

// Grab the values
$values = $hook->getValues();

// Assign variables
$subject = $values['subject'];
$redirect = $values['redirect'];
$tpl = $values['tpl'];

// Check MODX console that the values were captured correctly.
$modx->log(modX::LOG_LEVEL_ERROR, 'Subject is: ' . $subject);

// Set formit options
$hook->formit->options['emailSubject'] = $subject;
$hook->formit->options['redirectTo'] = $redirectTo;
$hook->formit->options['emailTpl'] = $tpl;

return true;

This could be it. Only thing I could think of is the cached placeholders.

Well I tried [[!+ uncached calls for the placeholders too, didn’t work, Nightmare isn’t!
Driving me crazy! :crazy_face:

ok mate I got errors:

[2019-05-03 08:03:36] (ERROR @ /home/user/folder/core/cache/includes/elements/modsnippet/66.include.cache.php : 12) Subject is: 
[2019-05-03 08:03:50] (ERROR @ /home/user/folder/core/components/formit/src/FormIt/Hook/Email.php : 303) [FormIt] An error occurred while trying to send the email. Could not instantiate mail function.

Then I tried filling subject with a default element:

[[!FormIt? 
    &preHooks=`formCustomFields`
    &hooks=`spam,email,redirect`
	&emailFrom=`[[+email]]`
	&emailFromName=`[[+firstname]] [[+lastname]]`
	&emailSubject=`subject Here`
.....

But the error stil the same.

Move the snipet into hooks:

&preHooks=``
&hooks=`formCustomFields,spam,email,redirect`

still not working.
Hope you are not running out of Ideas :joy:

I’ll have a more in depth look later but what I gave you was a hook. Because not even the subject value was printed out in the MODX console suggests that it’s not even picking up the values correctly for whatever reason. If you inspect your form in the browser can you see that the hidden input for subject has a value ?

Yes they have values:
in the code the values are called like this :

[[$contactForm? &subject=Web Message&redirect=23&tpl=emailChunk]]

contactForm Chunk:

	<input type="hidden" name="subject" value="[[+subject]]" />
	<input type="hidden" name="redirect" value="[[+redirect]]" />
	<input type="hidden" name="tpl" value="[[+tpl]]" />

And this is what I see if I press F12 (devTools) in my browser:

	<input type="hidden" name="subject" value="Web Message" />
	<input type="hidden" name="redirect" value="23" />
	<input type="hidden" name="tpl" value="emailChunk" />

So I can see the values are there, is it possible that the call:

 [[!FormIt? 
    &preHooks='formCustomFields'
    &hooks='[[++recaptcha:is=`Yes`:then=`recaptchav2,`]]spam,email,redirect'
    ...

Is being processed before the form is loaded? I try moving the form call at the bottom after the form but nothing happened.

the Subject empty error still there.

It’s a hook not a prehook. Can you try it as a hook?

&hooks='formCustomFields,email,redirect'

OK a small advance:
The email was sent :+1: good.

and we have a log, that means it’s reading the values:

[[$contactForm? &subject=‘Web Message’ &redirect=‘23’ &tpl=‘contactEmailChunk’]]

[2019-05-03 11:55:30] (ERROR @ /home/user/folder/core/cache/includes/elements/modsnippet/66.include.cache.php : 12) Subject is: Web Message

But it’s not applying any value in the formit options:

[[!FormIt? 
    &hooks=`formCustomFields,spam,email,redirect`
    ....
    &emailSubject=`replaced`
    &redirectTo=`1`
    &emailTpl=`replaced`
   ...

This one redirect to id 1, no template and show replaced as subject

so I tried deleting the default values:

[[!FormIt? 
    &hooks=`formCustomFields,spam,email,redirect`
    ....
    &emailSubject=``
    &redirectTo=``
    &emailTpl=``
   ...

This one no redirect, no template and show no subject.

If I delete the options ( &emailSubject, &redirectTo, &emailTpl), I got the same result as before.

But it’s something!!! :+1::grin:
Any idea why it’s not writing the options?

Thanks mate!

Well, I’m extremely happy to announce that it’s working!!!
Yeahhh!!! :+1::grin::+1:

Here the solution but first thank you very much to lkfranklin, lja-web and bobray for your help!

The solution was quite simple, just add the placeholders in the Formit call :neutral_face: Rubish isn’t!

so if you want to use placeholders as settings in your FormIt it’s quite easy:

1. Add the fields in your form that in this case it’s inside a chunk called: contactForm

	<input type="hidden" name="subject" value="[[+subject]]" />
	<input type="hidden" name="redirect" value="[[+redirect]]" />
	<input type="hidden" name="tpl" value="[[+tpl]]" />

2. Fill the placeholders with proper data

[[$contactForm? &subject='Web Message' &redirect='10' &tpl='contactEmailChunk']]

3. create a hook to read the data in the inputs.

<?php
$formit =& $hook->formit;
$values = $hook->getValues();

$subject = $values['subject'];
$redirect = $values['redirect'];
$tpl = $values['tpl'];

$formit->config['emailSubject'] = $subject;
$formit->config['redirectTo'] = $redirect;
$formit->config['emailTpl'] = $tpl;

return true;

4. and most important part , include freaking placeholders in your snippet call:

[[!FormIt? 
    &hooks=`formCustomFields,spam,email,redirect`
	&emailFrom=`[[+email]]`
	&emailFromName=`[[+firstname]] [[+lastname]]`
	&emailSubject=`[[+subject]]` // <<<< Like here
    &redirectTo=`[[+redirect]]` // <<<< here
	&emailTpl=`[[+tpl]]` // <<<< and here
...

5. Ta taaaan, It works!!! :+1:

Hope this save time to somebody else.
Enjoy!!!

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.