Forums

Dynamic email templates for dynamic form

Zahorijs 13 Mar, 2011
First of all, thank you for the great component you made!

I am using ChronoForms V3.2.0.

I made dynamic form based on tutorial in FAQs of this site. In my case "dynamic" means that at the beginning users can see fields (dimensions, weight) only for one cargo unit, but they can tick a checkbox and javascript will unhide 2nd (or 3rd, 4th, etc) cargo unit's fields. I made php loop that generates code for 20 cargo units. So users can order any amount of cargo form 1 to 20.

The problem is that user ordering 1 cargo unit will receive conformation e-mail with 19 empty cargo units' fields.

I thought, that putting following code in email template will solve my problem. As field variables I use {text_number}. I simplified my code so you can easier get the idea.

<?php
for ($unit = 1; $unit <= 10; $unit++) {
	$UnitValue = "{text_$unit}";
    if(stristr($UnitValue, "text") === FALSE) 
		{
		echo "<p><h3>Unit No. $unit</h3></p>";
		echo "<p><b>Weight:</b> {text_$unit}</p>";		
		}
    else
		break;
}
?>


I was hoping that loop will iterate trough {text_number} till it finds string "text", which will mean that this field was empty and no need to show the rest of the units to user, cause he/she did not fill them out.

But the email template loop did not work. I guess it is because ChronoForms populate field variables only after the php of email template had been processed. But that doesn't really help me to solve my problem.

I spent more then 5 hours going trough forums, but was unable to find a solution.

I would really appreciate if you could help me with this!
GreyHead 13 Mar, 2011
Hi Zahorijs ,

As you suspected you can't use the {input_name} syntax in PHP.

Instead use the Joomla! JRequest methods.
<?php
for ($unit = 1; $unit <= 10; $unit++) {
   $UnitValue = JRequest::getInt('text_'.$unit, '', 'post');
    if ( $UnitValue )
      {
      echo "<p><h3>Unit No. $unit</h3></p>";
      echo "<p><b>Weight:</b> $UnitValue</p>";      
      }
    else
      break;
}
?>
I've used the GetInt() method here; there are several others getString() getFloat(), getVar(), etc. each of which validates the result slightly differently.

Bob
Zahorijs 13 Mar, 2011
Thank you so much!
I lost 1 hour figuring out, how to get getWord to work, because I am very new to php and it's syntax. But then I remembered about "validate-number" and getInt was all that I needed🙂
This topic is locked and no more replies can be posted.