use PHP to create a data value

jj135 11 Dec, 2020
Hi! I just started working with CF7. So the setup / interface is a bit getting used to compared to CF6.

My question is: How do I create a custom data value in the data array so I can use this in the e-mail that is send. I would like tot use a PHP action to create this value,

The form type is set to 'advanced'. That way I can add an PHP action to the page.

When I add code:

$this->data("testing","test",true);

I would expect to be able to use this in the e-mail that is send:

{data:testing}

Should this work or do I need a different way to do this in CF7?

Kind regards,
Jip
jj135 11 Dec, 2020
Answer
2 Likes
Ah, I think I know what I missed. I need to put the action on the end_page page. I had it on the start_page page.

For anyone who might need this:

To create a custom variable / value in CF7 you can use the PHP action (only in 'advanced' form type - on the end_page page) to create:

1) A value in the data array:
$this->data("testing","test",true);

This will put the value in the data array and you can then use {data:testing} in an email to retrieve the value. In this example it would bet 'test'. But you can of course create any value you need in the PHP action.

2) A PHP variable. This returned variable will have the name of the PHP action (for example 'php_1')
$myvalue = "test"
return $myvalue;

So this will be stored as a variable you can then use like this:
{var:php_1}

Kind regards,
Jip
sem 04 Feb, 2021
Hi - interesting.

Say I have a two field, repeater area that produces an array. The repeater area is called Contact.

If I put {data:Contact} in the email form after entering two rows, I get

{"1":{"name":"Fred","choices":"5"},"2":{"name":"Tony","choices":"7"}}

How would I be able to format this output better so it could be inserted into the email in, say a tabular form?

So the output would be a bit like:

Name: Fred Choices: 5
Name: Tony Choices: 7

Using {data.pr:Contact} is better as all the rows are grouped together in a more readable fashion but it's still a raw output from the array.

I guess it would be using the php action and then the resulting formatted output would be inserted into the email template using the {var:php_action_name}?

mark
jj135 04 Feb, 2021
I believe you're dealing with a json string here. Not an array. But I am not sue.

You could maybe create an php array from the json string ($myJSON = json_encode($myObj);) and the loop through the array to create the output you want.

But I also think there is (or was) an option in CF to do this for you and output it as a comma seperated list.

Hope this helps. I have not tested this.
sem 04 Feb, 2021
Hi - I was looking maybe to use

extract();

to split the array $this->data("Contact") and format keys and values. Can't get it to work. But to be fair CF7 is new and there's loads of things being added to it all the time.
Post a Reply