Forums

Insert dynamicly made form fields into the email template ?

Jefftech 23 Nov, 2009
hi, I'm having a problem with inserting dynamically created hidden fields in to the email.

ie: when the form runs it goes through a for loop and creates fields

<input value="0x937" id="code0" name="code0" type="hidden" />
<input value="0x937" id="code1" name="code1" type="hidden" />
etc ....

So a number of fields are generated dynamically.

I can get those fields in the email just fine as well {code0} {code1} etc ..

My issue is to do that right now I'm manually putting them into the template. If there are only 2 fields produced on the form and I put 10 in the template . I end up with the proper data for the first 2, then {code3}{code4}.....{code9}{code10}. For fields that didn't get generated.

I need the template to only show the fields that were produced. There is no way to tell before hand how many fields will get generated. To put each one into the email template manually.

Any help would be greatly appreciated.
GreyHead 23 Nov, 2009
Hi jefftech,

You'll need to do it dynamically in the template as well. Check the entries in the $_POST array and add the ones that exist into the template.

Bob
Jefftech 23 Nov, 2009
Just a little confused as to where I would put the code to check the $_POST array ?
Would i just do that inside the template like this ?

If the dynamic field is code0 in this case....

{field1}
{field2}
{field3}

<?php 
if (isset($_POST['code0'])){
echo "{code0}"; 
}
?>
GreyHead 23 Nov, 2009
Hi Jefftech,

Something like that. You need to turn of the HTML EDitor in the Email Setup properties to get a plain text area to work in.

You also can't combine the {field_name} syntax with PHP as the processing order doesn't work correctly.

The preferred Joomla syntax for reading the $_POST array is
$field_name = JRequest::getVar('field_name', 'default_value', 'post');
You can replace getVar with getString, getInt, and a few others to improved the filtering.

Bob
Jefftech 24 Nov, 2009
Ok so if I understand you correctly.

If I had the following produced on the form.


<input type="text" name="firstname" value="fname"><br> 
<input type="text" name="lastname" value="lname"><br>
<input type="text" name="code0" value="1"><br> 
<input type="text" name="code1" value="2"><br>
<input type="submit" value="submit">


Then in the template, with the editor turned off I could put ...

  
<?php

$firstname = JRequest::getVar('firstname', 'default_value', 'post');
$lastname= JRequest::getVar('lastname', 'default_value', 'post');
$code0= JRequest::getVar('code0', 'default_value', 'post');
$code1= JRequest::getVar('code1', 'default_value', 'post');

echo $firstname."<BR>";
echo $lastname."<BR>";

if (isset($code0)){
  echo $code0."<BR>";
}
if (isset($code1)){
  echo $code1."<BR>";
}

?>


to produce the following in the email ?


fname
lname
1
2


ps. the values and fields are just examples.

I know how to do what i want very easily in straight php but doing it in joomla/chronoforms is another matter entirely .

Thanks alot for your help on this Bob.
GreyHead 24 Nov, 2009
Hi jefftech,

That looks good to me. Of course you can be cleverer with the PHP to automate the checking with a for loop.

Bob
Jefftech 24 Nov, 2009
Ok I did as above, put it in the emails templates section with the HTML Editor turned off.

I get this error.

You must provide at least one recipient e-mail address.

In the setup emails section I do have all the required fields set as I was originally just using the template editor, and it worked fine.

If I put php in the emails templates section it won't use the dynamic to/from/BCC etc.. from the setup emails section to send the email ?

Unsure how to proceed from here.

Do I construct and send an email as you would in any regular php script ?
mail($recipient, $subject, $message, $headers);

And where would i put that?
GreyHead 24 Nov, 2009
Hi Jefftech,

Provided the PHP isn't throwing any errors then PHP in the template should have no effect on the Email Setup - dynamic fields should work just as they did before.

Provided also that you aren't changing the values of any of the paramters used in the Dynamic fields. Is that possible?

In the Site Global Congiguration try setting Error Reporting to Maximum temporarily. It's possibl that there's a PHP error that is breaking soem part of the ChronoForms email process.

Bob

PS You can use the PHP or Joomla mail functions but you shouldn't need to.
Jefftech 24 Nov, 2009
tracked it down I had a variable name in the template that was used elsewhere in chronoForms - i think in mails.php But i changed it and works properly now.

So everything works as intended now.

Thanks so much for you help Bob. Greatly appreciate it.
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger