PDA

View Full Version : Use number as variable in template



piemanek
15-Dec-2006, 01:42 PM
Hi

One of my form fields is a number, which I would then like to use as a variable in my template (which is a php file).

Is this possible? At the moment, the $number generates output, but I can't seem to use it as a variable. For example:

<?php echo '$number'; ?> works fine,
but <?php for ($count; $count<=$number; $count++) { ?> does not work.


Thanks!

russellr
15-Dec-2006, 08:01 PM
Hi,

The problem is the way PHP works and the timing of FormMail templates.

You're using $TEMPLATEURL, right? You must be otherwise the PHP would not be getting executed.

So, what's happening is this:
FormMail opens your template (your PHP script) just like a browser would and the server executes the PHP script.
The output of the PHP script (which is straight HTML) is read by FormMail.
FormMail processes the HTML output (the template as far as it is concerned), substituting field values.
The changed HTML is sent to the user's browser.

<?php echo '$number'; ?> works fine,

This only works fine because your PHP script is sending $number (literally) in its output. If you execute your PHP script in your browser, that's what you'll see come out.

If you change it to this (using double quotes instead of single quotes) it will stop working:



<?php echo "$number"; ?>

If you understand the difference between double quotes and single quotes in PHP, then you'll understand why.

So, how do you solve this problem?

There only two ways to do this:
perform the calculations using the FormMail Computation Module and substitute the result fields in the template (don't use PHP in the template)
use the FormMail hook system to include your own PHP code in FormMail, generate result fields there and then substitute them in the templateThe FormMail Computation Module is not a free product, but it's a good one. :)