Buy Now
Sign in

Remove form fields without content from data array before mail sent

Erik66 72
May 20 2021, 20:57 #396533
Hello all,
I have a form where I want to remove all empty fields from the {data:} array after the form is posted and before any email is sent, as I do not wish any empty fields to appear in the email. How do I go about that? Does someone have a suggestion on how to achieve that?

Thanks in advance
GreyHead 65
May 22 2021, 09:14 #396539
Hi Erik,
You could use custom PHP to loop through the form data array and unset any empty values. I think that will do what you need.
If you'd like to buy me a coffee or two, thank you very much
Erik66 72
May 22 2021, 11:10 #396540
Hi Bob, thank you for your suggestion. I've actually tried something similar using the PHP array_filter() function. In the actions, I added the following custom code prior to any action:

$this->data = array_filter($this->data, function($value) { return !is_null($value) && $value !== ''; });
This code succesfully removes any key/value pair where the value is empty. I checked that by then adding a print_r($this->data) before and after the above function, and comparing the results.
However, the email sent as an action after this custom code, contains all data from the original data array, including the key/value pairs with no values. Which makes me think I either used the wrong data array, or the email action fetches the data array prior to the action, or.... ?