Forums

How to add some variable into the e-mail

H13 04 Jun, 2008
Hi, is there a way how to add a variable into the e-mail, e.g. I have a select form:
<select name="sel">
<option value="0" selected="selected">- Select -</option>
<option value="1">Great</option>
<option value="2">Good</option>
</select>
Then I can get and change the code which will be displayed as confirmation (On Submit code - before sending email)
if (isset($_POST['sel'])) {
  if ($_POST['sel'] == 1) {
    $_POST['sel'] = 'You have selected 'Great'';
    // or  $sel = 'You have selected 'Great';
}

echo $_POST['sel'];
// echo $sel;
// this return: You have selected 'Great'...
BUT if I add the {sel} code into the e-mail template, I get only '1' not 'You have selected 'Great''

I hope, you understand,

thank you, Jan
GreyHead 05 Jun, 2008
Hi Jan,

Hmmm . . . it seems that the 'OnSubmit before' code is executed after the template is parsed (this isn't the sequence that I expected - I wonder if it has changed?)

There are at least three ways round this.

a) The simplest is to set the Select values
<select name="sel"> 
<option value="0" selected="selected">- Select -</option> 
<option value="Great">Great</option> 
<option value="Good">Good</option> 
</select>
and put
'You have selected '{sel}'
into the template.

b)You could hack chronocontact.php and move the 'OnSubmit before' code
   /**
     * Run the On-submit 'pre e-mail' code if there is any
     */
    if ( ! empty( $rows[0]->onsubmitcodeb4 ) ) {
        eval( "?>" . $rows[0]->onsubmitcodeb4 );
    }
from around line 395 to around line 262
// <-- insert here
   
    /**
     * If there are no errors and e-mail is required then build and send it.
     */
all references to the Joomla 1.0.x version, Joomla 1.5 may be slightly different.

OR

c) Without hacking the code you could create a little substitution of your own by putting a distinctive code into the template like ##sel## and substituting this in the OnSubmit before box:
<?php
$sel = "";
$sel_array = array(1 => "Great", 2 => 'Good');
if ( isset($_POST['sel']) ) { 
    $sel = "You have selected '".$sel_array[$_POST['sel']]."'"; 
} 
$html_message = str_replace( "##sel##", $sel, $html_message );
?>
Bob
H13 05 Jun, 2008
The c) would be great, but:

$html_message = str_replace( "##sel##", $sel, $html_message );

doesn't work for me :-(

Jan
GreyHead 05 Jun, 2008
Hi Jan,

Tested this time - I missed a ] in
$sel = "You have selected '".$sel_array[$_POST['sel']]."'"; 
Bob
H13 05 Jun, 2008
Yes, but this is not the problem, I have added it and it still not worked :-(
GreyHead 05 Jun, 2008
Hi Jan,

Sorry, it works OK for me - off to lunch now. I'll post my test form when I'm back.

Bob
GreyHead 05 Jun, 2008
Hi Jan,

Here's the code I used. Form HTML:
Name: <input name="name" value="" type="text"><br />
E-mail: <input name="email" value="" type="text"><br />
<select name="sel">  
<option value="0" selected="selected">- Select -</option>  
<option value="1">Great</option>  
<option value="2">Good</option>  
</select>
<input name="submit" value="Submit" type="submit">
Onsubmit before email:
<?php 
$sel = ""; 
$sel_array = array(1 => "Great", 2 => 'Good'); 
if ( isset($_POST['sel']) && $_POST['sel'] != '- Select -') {  
    $sel = "You have selected '".$sel_array[$_POST['sel']]."'";  
}  
$html_message = str_replace( "##sel##", $sel, $html_message ); 
?>
Email template
Select using bracket syntax: {sel}<br /> 
Select using ## syntax: ##sel##
and the result is
Select using bracket syntax: 1
Select using ## syntax: You have selected 'Great'
Bob [file name=test_form_14.cfbak size=2143]http://www.chronoengine.com/images/fbfiles/files/test_form_14.cfbak[/file]
H13 05 Jun, 2008
thank you, the test_form 14 work for me, but in my form I still get this error:

Notice: Undefined variable: html_message in D:\www\J153\components\com_chronocontact\chronocontact.php(447) : eval()'d code on line 3



EDIT: the problem was that I disabled the email sending for the testing the form...

it works now, thank you very much, Jan

SOLVED
GreyHead 05 Jun, 2008
Hi Jan,

Great - glad it's working for you.

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

VPS & Email Hosting 20% discount

{item:title} {images:#}