Sending Dynamic To eMail to an email address fetched from a mysql database (with DB Read)

gatekeepa123 08 Feb, 2017
I'm working on a solution for the following problem:

I need to have one chronoforms form sending an email to a generic email adress
and furthermore to a specific address, which I want to get with the DB read function from a mysql database.

In my understanding - I dont know, if I'm right here - I need to specify a form field in the "Dynamic To" Field under the "Advanced Tab" in the email action.

As a test I tried putting a hidden form field unter "Code/Custom Code" and everything is working:

$dynamic_to = "testmail@testhost.com";
<input name="dynamic_to" id="dynamic_to" value="<?php echo $dynamic_to; ?>" type="hidden" class="form-control A" 


The DB Read function works aswell: I can fetch the array which holds the email, strip the email to a variable and echo the email address with Custom Code Custom Code in the "DB Read / On Found" section.

If I try to use the variable with the email in the custom form code however, the variable is empty.

So my question is: How can I use the data that I got via the DB Read function in the form section?
Or: Is there a better way around without having to working with hidden fields?

Thank you very much in advance!
Max_admin 09 Feb, 2017
Hi gatekeepa123,

The db read data are fetched in the $form->data array, so you can just use this in your dynamic to field:
{model.field} or {field}

The existence of "model" depends on if you have the "Enable model id" setting in the db read settings enabled or disabled.

Best regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
gatekeepa123 09 Feb, 2017
Hey Max,

thank you very much for your reply.

I think the problem is, that I need to strip the eMail from a $form->data array field and I cant use that new variable in the dynamic To field.

The data I get with the DB Read function is an array with the following scheme (Model ID="Data6"):

Array ( [Data6] => Array ( [0] => Array ( 
[id] => 1
[elements] => { "sometextsometext HERE_IS_THE_EMAIL@testhost.de sometextsometext... }
Array ( [Data6] => Array ( [0] => Array ( 
[id] => 2
[elements] => { "sometextsometext HERE_IS_THE_EMAIL@testhost.de sometextsometext... }


___

My custom code to strip the eMail from the elements field:

$elements_string = $form->data['Data6'][$array_id]['elements'];
$elements_cut = substr($elements_string, 67);
$pos = strpos($elements_cut, '"');  
$email_address = substr($elements_cut, 0, $pos); 


Is there any way to use the new $email_adress variable in the Dynamic To field?

Best regards,
Marc
GreyHead 09 Feb, 2017
Hi Marc,

Please change this line
$email_address = substr($elements_cut, 0, $pos);
to
$form->data['email_address'] = substr($elements_cut, 0, $pos);
and then put email_address in the Dynamic To box - without any quotes or brackets.

Bob
gatekeepa123 09 Feb, 2017
I changed that and I can echo $form->data['email_address']; which contains the correct eMail adress.

I also changed the "Dynamic To" field to email_address and also tried {email_address), which Max suggested, but it doesnt seem to work:

Data Array

Array
(
    [chronoform] => KontaktDBRead
    [event] => submit
    [name] => Test
    [button4] => Submit
    [dynamic_subject] => Test
    [dynamic_reply_to_name] => Test
    [dynamic_reply_to_email] => replymail@pixeltopf.com
    [dynamic_from_name] => Test
    [dynamic_from_email] => test@pixeltopf.com
    [ip_address] => xxx
)


Debug Info

Array
(
    [8] => Array
        (
            [Email] => Array
                (
                    [0] => An email with the details below was sent successfully:
                    [1] => To:, test@pixeltopf.com
                    [2] => Subject:Test
                    [3] => From name:Test
                    [4] => From email:test@pixeltopf.com
                    [5] => CC:
                    [6] => BCC:
                    [7] => Reply name:dynamic_reply_to_name
                    [8] => Reply email:dynamic_reply_to_email
                    [9] => Attachments:
                    [10] => Array
                        (
                        )

                    [11] => Body:
<div class="form-group gcore-form-row" id="form-row-name"><label for="name" class="control-label gcore-label-left">Name:</label>
<div class="gcore-input gcore-display-table" id="fin-name">Test</div></div><div class="form-group gcore-form-row" id="form-row-button4"><div class="gcore-input gcore-display-table" id="fin-button4"></div></div>


<br /><br />IP: xxx
                )

        )

)
GreyHead 09 Feb, 2017
Hi Marc,

Is the Custom Code action before the Email action? If not, please drag it up.

Bob
gatekeepa123 09 Feb, 2017
Hey Bob,

yes it is:

On load - DB Read - On found - Custom Code
...and afterwards:
On Submit - Email

Marc
GreyHead 09 Feb, 2017
Answer
Hi Marc,

If the DB Read is in the On Load event how are you passing the value of the email to the On Submit event?

Bob
gatekeepa123 10 Feb, 2017
Hey Bob,

that was the problem: I thought variables are shared between different events, my fault!
I put my DB Read / Custom Code function into the On Submit event and everything works as planned.

Thank you very much!

Marc
GreyHead 10 Feb, 2017
Hi Marc,

They are not shared by default - you can pass them as hidden inputs in the form, or by saving them to the user session, or call them up - as you have now done - in the event where you need them.

Bob
This topic is locked and no more replies can be posted.