Forums

Output php variable in the email template

seabass 10 Sep, 2009
Hi,
I have created a form and on submission I have used php to generate a random number which will be used as a booking reference.
The php code resides in the "On Submit code - before sending email:" section and reads as follows:
<?php
$booking = mt_rand() . "\n";
echo $booking;
?>

On submitting the form my random number is successfully generated and appears within a new content window.

However how do I echo the random number or $booking variable into my email template? or alternatively into the email subject?
I have tried
{$booking} however this doesn't work.

I appreciate any help.
Thanks
GreyHead 10 Sep, 2009
Hi seabass,

Add it to the $_POST array
JRequest::setVar('booking', $booking);
Then you can get it back later using
$booking = JRequest::getInt('booking', '', 'post');
echo $booking;

Bob
seabass 10 Sep, 2009
Hi Bob,
Thanks for your quick reply.
My on submit code now reads
<?php
$booking = mt_rand() . "\n";

$_POST=array (JRequest::setVar('booking', $booking);
$booking = JRequest::getInt('booking', '', 'post');
echo $booking;
?>

I have then tried to call the value using {booking} in the email template.
However I get the error:
Parse error: syntax error, unexpected ';', expecting ')' in /home/korrie/public_html/components/com_chronocontact/chronocontact.php(371) : eval()'d code on line 4

I appreciate your help.
Huw
seabass 10 Sep, 2009
My form html code looks like this:
<table width="800" border="0" cellpadding="10" cellspacing="1">
    <tr>
      <td bgcolor="#CCCCCC"><h1 align="left">Your details </h1>
        <p align="left"><strong>Full name </strong><br />
            <input name="fname" type="text" size="50" />
        </p>
        <p align="left"> <strong>Email</strong><br />
            <input name="email" type="text" size="50" />
        </p>
               <p align="left"> <strong>Telephone</strong> <br />
            <input type="text" name="tel" />
        </p>
        <hr align="left" />
        <h3 align="left"><strong>Dates and times you require the room </strong></h3>
        <p align="left"><strong>From:</strong><br />
          Day
          <select name="dayto">
      <option selected="selected">1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
      <option>6</option>
      <option>7</option>
      <option>8</option>
      <option>9</option>
      <option>10</option>
      <option>11</option>
      <option>12</option>
      <option>13</option>
      <option>14</option>
      <option>15</option>
      <option>16</option>
      <option>17</option>
      <option>18</option>
      <option>19</option>
      <option>20</option>
      <option>21</option>
      <option>22</option>
      <option>23</option>
      <option>24</option>
      <option>25</option>
      <option>26</option>
      <option>27</option>
      <option>28</option>
      <option>29</option>
      <option>30</option>
      <option>31</option>
    </select>
          Month
  <select name="monthto">
    <option>January</option>
    <option>February</option>
    <option selected="selected">March</option>
    <option>April</option>
    <option>May</option>
    <option>June</option>
    <option>July</option>
    <option>August</option>
    <option>September</option>
    <option>October</option>
    <option>November</option>
    <option>December</option>
  </select>
          Year
  <select name="yearto">
    <option selected="selected">2009</option>
    <option>2010</option>
    <option>2011</option>
  </select>
        Start Time:<strong>
<select name="starthr" id="starthr">
  <option>7</option>
  <option>8</option>
  <option selected="selected">9</option>
  <option>10</option>
  <option>11</option>
  <option>12</option>
  <option>13</option>
  <option>14</option>
  <option>15</option>
  <option>16</option>
  <option>17</option>
  <option>18</option>
  <option>19</option>
  <option>20</option>
  <option>21</option>
  <option>22</option>
</select>
        </strong>
        <select name="startmin" id="startmin">
          <option selected="selected">00</option>
          <option>15</option>
          <option>30</option>
          <option>45</option>
        </select>
        <br />
        </p>
        <p align="left"><strong>To:</strong><br />
          Day
          <select name="dayfrom">
      <option selected="selected">1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
      <option>6</option>
      <option>7</option>
      <option>8</option>
      <option>9</option>
      <option>10</option>
      <option>11</option>
      <option>12</option>
      <option>13</option>
      <option>14</option>
      <option>15</option>
      <option>16</option>
      <option>17</option>
      <option>18</option>
      <option>19</option>
      <option>20</option>
      <option>21</option>
      <option>22</option>
      <option>23</option>
      <option>24</option>
      <option>25</option>
      <option>26</option>
      <option>27</option>
      <option>28</option>
      <option>29</option>
      <option>30</option>
      <option>31</option>
    </select>
          Month
  <select name="monthfrom">
    <option>January</option>
    <option>February</option>
    <option selected="selected">March</option>
    <option>April</option>
    <option>May</option>
    <option>June</option>
    <option>July</option>
    <option>August</option>
    <option>September</option>
    <option>October</option>
    <option>November</option>
    <option>December</option>
  </select>
          Year
  <select name="yearfrom">
    <option selected="selected">2009</option>
    <option>2010</option>
    <option>2011</option>
  </select>
  End Time:
  <strong>
  <select name="endhr" id="endhr">
            <option>7</option>
            <option>8</option>
            <option selected="selected">9</option>
            <option>10</option>
            <option>11</option>
            <option>12</option>
            <option>13</option>
            <option>14</option>
            <option>15</option>
            <option>16</option>
            <option>17</option>
            <option>18</option>
            <option>19</option>
            <option>20</option>
            <option>21</option>
            <option>22</option>
          </select>
          </strong>
          <select name="endmin" id="endmin">
            <option selected="selected">00</option>
            <option>15</option>
            <option>30</option>
            <option>45</option>
          </select>
        </p>
<hr align="left" />
        <h3 align="left"><strong>Type of room required</strong> - if the selections don't, match please type in "Other". </h3>
        <p align="left">
          <label>
            <input type="radio" name="radio1" id="Office" value="Office" />
          </label>
        Office</p>
        <p align="left">
          <label>
            <input type="radio" name="radio1" id="Meeting" value="Meeting" />
            Meeting</label>
        </p>
        <p align="left">
          <label>
            <input type="radio" name="radio1" id="Conference" value="Conference" />
            Conference</label>
        </p>
        <p align="left">Other
          <input name="othertype" type="text" size="50" />
        </p>
        <h3 align="left"><strong>Approximate number of persons using the room</strong> (this will help us find the best room for you)</h3>
        <p align="left">
          <label>
            <input type="radio" name="radio2" id="One_Twenty_Persons" value="One_Twenty_Persons" />
            1-20 persons</label>
          <label>
             <input type="radio" name="radio2" id="Twenty_Forty_Persons" value="Twenty_Forty_Persons" />
            20-40 persons</label>
          <label> </label>
        </p>
        <p align="left">
          <label>
            <input type="radio" name="radio2" id="Forty_Eighty_Persons" value="Forty_Eighty_Persons" />
          40-80 persons</label>
          <label>
            <input type="radio" name="radio2" id="Eighty_plus" value="Eighty_plus" />
            80 + persons</label>
        </p>
<hr align="left" />
        <h3 align="left"><strong>Room facilities required. </strong>(please tick  boxes) </h3>
        
        
        <div align="left">
          <table width="600" border="0" cellspacing="1" cellpadding="5">
            <tr>
              <td valign="top" bgcolor="#CCCCCC"><strong>Furniture and layout </strong></td>
              <td valign="top" bgcolor="#CCCCCC"><strong>Equipment</strong></td>
              <td valign="top" bgcolor="#CCCCCC"><strong>Service</strong></td>
            </tr>
            <tr>
              <td valign="top"><p>
                  <input type="checkbox" name="tables" value="tables" />
                  Tables </p>
                  <p>
                    <input type="checkbox" name="chairs" value="chairs" />
                    Chairs </p>
                <p>
                    <input type="checkbox" name="workstations" value="workstations" />
                  Workstations</p>
                <p> </p></td>
              <td valign="top"><p>
                  <input type="checkbox" name="projector_screen" value="projector_screen" />
                  Projector and Screen</p>
                  <p>
                    <input type="checkbox" name="audio_system" value="audio_system" />
                    Audio system</p>
                <p>
                    <input type="checkbox" name="microphone" value="microphone" />
                  Microphone</p>
                <p>
                    <input type="checkbox" name="internet_access" value="internet_access" />
                  Internet access</p>
                <p>
                    <input type="checkbox" name="ohp_screen" value="ohp_screen" />
                  OHP and Screen</p>
                <p>
                    <input type="checkbox" name="interactive_wb" value="interactive_wb" />
                  Interactive Whiteboard</p>
                <p>
                    <input type="checkbox" name="whiteboard" value="whiteboard" />
                  Whiteboard</p>
                <p>
                    <input type="checkbox" name="tv_dvd" value="tv_dvd" />
                  TV and DVD</p></td>
              <td valign="top"><p>
                  <input type="checkbox" name="full_catering" value="full_catering" />
                  Full Catering</p>
                  <p>
                    <input type="checkbox" name="tea_coffee" value="tea_coffee" />
                    Tea and Coffee</p></td>
            </tr>
          </table>
          <br />

            <label>Other requirements:
              <input name="other_facilities" type="text" id="other_facilities" size="75" />
            </label>
  
        </div>
        <p align="left"><strong>Anti Spam -Please enter the text in black </strong></p>
        <p align="left">{imageverification} </p>
        <p align="left">
          <input type="submit" name="Submit" value="Submit"/>
        </p></td>
    </tr>
  </table>
GreyHead 10 Sep, 2009
Hi seabass,

The error is becasue you missed a ) in this line
$_POST=array (JRequest::setVar('booking', $booking);
but in fact the line is wrong so please delete it.

Becasue of the way ChronoForms works* you can't define a variable in the OnSubmit before code and them use it in the template with the {booking} syntax. Instead use <?php echo $booking; ?> in the template.

Bob

* the value substitution is done before the OnSubmit code is executed so ChronoForms doesn't know what the value is :-(
seabass 10 Sep, 2009
Hi Bob
I actually solved it in the meantime using this in my form:
</label><input name="booking" type="hidden" value="<?php $booking = mt_rand() . "\n"; print $booking;?>" />

I can see you had a similar workaround!

Then outputting {booking} in the email template

Next question!
Is there any way I can put the {booking} ref in the email subject, I tried a dynamic subject with "booking" but it seems to stop the form from sending.
seabass 10 Sep, 2009
Sorry ignore my last post.
I missed the enabled: yes button.
silly me.

Thanks for your help Bob
Really great forum and support.
Huw
GreyHead 10 Sep, 2009
Hi seabass,

A dynamic subject with just booking (no brackets, no quotes) shoudl work OK.

The problem may be the /n in the booking string as the subject can only be a single line I think. Try
<input name="booking" type="hidden" value="<?php echo mt_rand(); ?>" />

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