email template - php not working?

RemoP 02 Oct, 2011
Hi,

I'm trying to get PHP working in my email template, but for some reason it doesn't work..

I'm using 4.0 RC2.0 in Joomla 1.7.1, and so far I've had a great experience using chronoforms!🙂

I'm now playing around with a form I used with a different form builder, and am trying to 'clean up' the email submission by removing empty values from the email that is being sent.

The fields that are being used are either regular text fields or select boxes. When I display the values of the fields, I can see them just fine, but as soon as I implement php, I'm only receiving an email with an ip-address, and nothing else (Not even the basic text!).

Here is the code-snippet in the email template:

<style>
table{ border: 1px solid #ccc; }
table th { background-color: #ccc; }
</style>
Beste {Naam1},<br />
<br />
Some random text
<br />
<table cellpadding="0" cellspacing="0" border="0">
  <tr>
    <th align="left">Deelnemer details:</th><th></th>
  </tr>
  <tr>
    <td><b>Jou naam</b></td><td> {Naam1}</td>
  </tr>
<?php
if({Partner} != ''){
  echo '<tr><td>Partner</td><td>{Partner}</td></tr>';
}
if({kind1} != ''){
  echo '<tr><td>Kind</td><td>{kind1}</td></tr>';
}
if({kind2} != ''){
  echo '<tr><td>Kind</td><td>{kind2}</td></tr>';
}
?>
</table>
<table cellpadding="0" cellspacing="0" border="0">
  <tr>
    <th align="left">Trip details:</th><th></th>
  </tr>
<?php
if({regelen} == 'Ja') {
  echo '<tr><td>U heeft er voor gekozen de trip zelf te regelen.</td></tr> ';
} else {
  echo '<tr><td>Heenreis</td><td>{Heenreis}</td></tr>';
  echo '<tr><td>Terugreis</td><td>{terugreis}</td></tr>';
}
?>
</table>


I already found out that if/when I remove the word php from the start of my php block, it works partially, but then after the first echo it thinks that my php statement ended and will print the rest of the commands in the email without any further interpretation..

Here is an example of what I see in the email:

'; } if(name3 != ''){ echo ' '; } if( != ''){ echo ' '; } ?>
Deelnemer details:
Jou naam name1
Partner name2
Kind name3
Kind
'; } else { echo ' '; echo ' '; } ?>
Overtocht details:
U heeft er voor gekozen de trip zelf te regelen.
Heenreis 2
Terugreis 8



I'm just wondering if I'm overlooking something...

Thanks for any help you can offer.

Thanks, Remo.
GreyHead 02 Oct, 2011
Hi Remop,

You can't use the 'curly brackets' syntax in PHP because the PHP evalution is done before the curly-replacer is run. Instead use $form->data['input_name'] in the PHP so this
if({kind1} != ''){
  echo '<tr><td>Kind</td><td>{kind1}</td></tr>';
}
becomes
if( isset($form->data['kind1']) && $form->data['kind1'] != '' ) {
  echo '<tr><td>Kind</td><td>'.$form->data['kind1'].'</td></tr>';
}

Bob
RemoP 02 Oct, 2011
Excellent! Thanks for your swift reply Bob! Works like a charm!

I knew it would be something silly I did wrong.🙂

Much appreciated!

Regards, Remo.
nnasoody 16 Nov, 2011
Hi GreyHead,

I've had issues with the processing order as well and came across your answers on a number of posts with similar issues. However, I cannot retrieve the field value using the $form->data['input_name'];
It consistently returns null value when in fact there is a value stored in the database for that field.

In particular, I am attempting to retrieve the name of a user from the jos_users table using the user id stored in a chronoform table. Here's my code snippet:
<?php 
$db =& JFactory::getDBO();
$query = "SELECT users.`name`
	FROM `#__users` as users
	WHERE users.`id`=" . $form->data['cf_user_id'];
$db->setQuery($query);
$data = $db->loadObjectList();

// using jdump to examine the content
dump($data);
?>


where cf_user_id is the column name in the table.

I very much appreciate any help you can provide.

Thanks,
Nima
GreyHead 29 Nov, 2011
Hi Nima,

If you have a User ID in $form->data['cf_user_id'] then you can use the Joomla! User object to get the name
<?php
$user =& JFactory::getUser($form->data['cf_user_id']);
echo $user->name;
?>

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