Community

emailCC with FormIt sending via hook, triggers when it should not

So I have a very strange issue, I have a contact form with a option to indicate interest in a other product (using a checkbox, checkboxCCparts). If users check it a CC should be send as with the email.

I use a hook: checkboxCCparts and it works fine every time I test.

But the client keeps sending me mails where the CC is send when the option contact__interested_complete_vehicles (triggers the checkboxCCparts hook) is set to NO, but the CC is still send!
When I test I can not reproduce this at all but he keeps showing me the CC sometimes gets send when it should not.

I have no idea how this is happening, does anybody see a bug? or know a better way to do this?

My Ajax Form

[[!AjaxForm?
&snippet=`FormIt`
&hooks=`spam,recaptchav2,checkboxCCparts,email`
&form=`dealer_form_parts`
&validationErrorMessage=`[[%drt.something_went_wrong]]`
&successMessage=`[[%drt.message_send]]`
&emailTpl=`dealer_form_parts_email`
&emailHtml=`1`
&emailSubject=`[[*dealer_form_subject]]`
&emailTo=`info@company-email.com`
&emailCC=`[[+emailCC]]`
&emailFrom=`[[++email_parts_from]]`
&fsFormTopic=`Parts form`
&validate=`company:required,
    contact_person:required,
    address:required,
    postal:required,
    city:required,
    country:required,
    tel_office:required,
    contact__email:required,
    terms_01:required,
    terms_02:required`
]]

The form field to trigger CC

<label>[[%drt.contact__interested_complete_vehicles]] [[!+fi.error.contact__interested_complete_vehicles]]</label>
<div class="checkbox_container">
    <input type="hidden" name="contact__interested_complete_vehicles[]" value="" />
    <input type="checkbox" name="contact__interested_complete_vehicles[]" value="[[%drt.contact__yes]]" tabindex="19" [[!+fi.contact__interested_complete_vehicles:FormItIsChecked=`[[%drt.contact__yes]]`]]> [[%drt.contact__yes]]  
</div>

checkboxCCparts snippet for the hook

<?php
$contact__interested_complete_vehicles = $hook->getValue('contact__interested_complete_vehicles');
if (!$contact__interested_complete_vehicles) {
    $email = ""; 
} else {
    $email = "info@trading-email.com";
}
$hook->setValue('emailCC', $email);
return true;

If your input has a name of contact__interested_complete_vehicles[], that means $contact__interested_complete_vehicles will have a value of array('') or array('','yes'). An array like that is a truthy value, meaning that when cast to a boolean it is true, so your hook will always go into the else clause.

I’d recommend changing the form field to not use the array naming (the [] at the end), but simply set it to name="contact__interested_complete_vehicles" on both inputs. That way, the field will have a value of either "" or "yes", and casting "" to boolean results in false, making your hook work as expected.

2 Likes

Thanks Mark will adjust.