Sorry for posting such a common question. To hide empty fields within an email I've used the below code
But I'm doing a form now that is a standard submit form but is really long and was hoping for a way to not send the empty fields in the email without having to enter the above code on every line. I remember reading a while back that it's possible to have a script that checks the field values that were dumped into a DB table and only send the un-empty fields in the email but I was unable to find that post in the forums.
This form's values are being dumped into a DB table which is already set up.
A push in the right direction would be greatly appreciated,
Wesley
<?php
if ( $_POST['name'] > 0 ) {
echo "Name: ".$_POST['name']."<br />";
}
if ( $_POST['email'] > 0 ) {
echo "Email: ".$_POST['email']."<br />";
}
etc.....
?>
But I'm doing a form now that is a standard submit form but is really long and was hoping for a way to not send the empty fields in the email without having to enter the above code on every line. I remember reading a while back that it's possible to have a script that checks the field values that were dumped into a DB table and only send the un-empty fields in the email but I was unable to find that post in the forums.
This form's values are being dumped into a DB table which is already set up.
A push in the right direction would be greatly appreciated,
Wesley
Hi Wesley,
You can do it with an array of input names and default values:
Bob
Later: added missign $ sign on line 2
You can do it with an array of input names and default values:
<?php
$input_array = array(
'name_1' => 'default_value_1',
'name_2' => 'default_value_2',
'name_3' => 'default_value_3',
. . .
);
foreach ( $input_array as $k => $v ) {
$temp = JRequest::getVar($k, $v, 'post');
JRequest::setVar($k, $temp);
}
?>
Bob
Later: added missign $ sign on line 2
Thanks Bob, that looks just what I need. I'm sorry, I've been playing around with it and am receiving the error:
To be certain, I put this in my email template? For my test I created a simple form with the text field names: Name, Email, Phone; and the code I currently have is:
Am I heading in the right direction? Thanks!,
Wesley
CF 3.2
Parse error: syntax error, unexpected '=' in components/com_chronocontact/libraries/customcode.php(64) : eval()'d code on line 2
To be certain, I put this in my email template? For my test I created a simple form with the text field names: Name, Email, Phone; and the code I currently have is:
<?php
input_array = array(
'Name' => 'Name',
'Email' => 'Email',
'Phone' => 'Phone'
);
foreach ( $input_array as $k => $v ) {
$temp = JRequest::getVar($k, $v, 'post');
JRequest::setVar($k, $temp);
}
?>
Am I heading in the right direction? Thanks!,
Wesley
CF 3.2
Hi Wesley & Bob,
There's a $ missing in front of input_array on line 2.
/Fredrik
There's a $ missing in front of input_array on line 2.
/Fredrik
Thanks Fredrik, I didn't notice that myself, that fixed the error.
I'm sorry, using the above form as an example, do I put the below code in the email template and therefore will pull the submitted values? Currently the emails are coming over blank.
Thanks!
I'm sorry, using the above form as an example, do I put the below code in the email template and therefore will pull the submitted values? Currently the emails are coming over blank.
<?php
$input_array = array(
'Name' => 'Name',
'Email' => 'Email',
'Phone' => 'Phone'
);
foreach ( $input_array as $k => $v ) {
$temp = JRequest::getVar($k, $v, 'post');
JRequest::setVar($k, $temp);
}
?>
Thanks!
Hi Wesley,
Apologies for my typo. The code goes in the OnSubmit Before Email box.
Bob
Apologies for my typo. The code goes in the OnSubmit Before Email box.
Bob
Awesome, got things working and am now just working on the formatting. Is it possible for me to rearrange my coding below so that if a field is empty it won't show the label?
On Submit code - before sending email:
Email Template:
On Submit code - before sending email:
<?php
$input_array = array(
'Name' => 'Name',
'Email' => 'Email',
'Phone' => 'Phone',
'City' => 'City',
'State' => 'State'
);
foreach ( $input_array as $k => $v ) {
$temp = JRequest::getVar($k, $v, 'post');
JRequest::setVar($k, $temp);
}
?>
Email Template:
<?php
echo "<b>Student:</b> ".JRequest::getString('Name', '', 'post')."<br />";
echo "<b>Email:</b> ".JRequest::getString('Email', '', 'post')."<br />";
echo "<b>Phone:</b> ".JRequest::getString('Phone', '', 'post')."<br />";
echo "<b>City:</b> ".JRequest::getString('City', '', 'post')."<br />";
echo "<b>State:</b> ".JRequest::getString('State', '', 'post')."<br />";
?>
Hi hominid4,
The answer is "Yes . . . but . . ." You have to generate the labels in the PHP too and then remove them from the template itself:
Bob
PS I changed your array to add 'User' to the values just so show that keys (input names from the form) and values (labels to be used in the email) were different.
Later: corrected a typo - see later posts.
The answer is "Yes . . . but . . ." You have to generate the labels in the PHP too and then remove them from the template itself:
<?php
$input_array = array(
'Name' => 'User Name',
'Email' => 'User Email',
'Phone' => 'User Phone',
'City' => 'User City',
'State' => 'User State'
);
$address = array();
foreach ( $input_array as $k => $v ) {
$temp = JRequest::getVar($k, '', 'post');
if ( $temp ) {
$address[] = "$v : $temp";
}
}
JRequest::setVar('address', implode('<br />', $address));
?>
Then use {address} in the template.Bob
PS I changed your array to add 'User' to the values just so show that keys (input names from the form) and values (labels to be used in the email) were different.
Later: corrected a typo - see later posts.
Awesome, that works perfect! Although I'm afraid I'm doing something wrong, when the email arrives it correctly only shows the filled fields but it's not pulling the values, it's coming over literally as:
User Name: Name
User Email: Email
User Phone: Phone
User City: City
User State: State
My inputs are in the form of:
<input class="cf_inputbox" maxlength="150" size="25" id="name" name="Name" type="text" />
<input class="cf_inputbox" maxlength="150" size="25" id="email" name="Email" type="text" />
Sorry for being so slow with this, I keep trying to figure things out on my own first before posting back.
- Wesley
User Name: Name
User Email: Email
User Phone: Phone
User City: City
User State: State
My inputs are in the form of:
<input class="cf_inputbox" maxlength="150" size="25" id="name" name="Name" type="text" />
<input class="cf_inputbox" maxlength="150" size="25" id="email" name="Email" type="text" />
Sorry for being so slow with this, I keep trying to figure things out on my own first before posting back.
- Wesley
Hi hominid4,
Oops, a little typo crept in there
Bob
PS I've corrected my original post.
Oops, a little typo crept in there
$address[] = "$v : $k";
should be$address[] = "$v : $temp";
Bob
PS I've corrected my original post.
This topic is locked and no more replies can be posted.