Forums

Another 'Hide Empty Fields in Email' question

hominid4 20 Dec, 2010
Sorry for posting such a common question. To hide empty fields within an email I've used the below code
<?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
GreyHead 21 Dec, 2010
Hi Wesley,

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
hominid4 21 Dec, 2010
Thanks Bob, that looks just what I need. I'm sorry, I've been playing around with it and am receiving the error:
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
nml375 21 Dec, 2010
Hi Wesley & Bob,
There's a $ missing in front of input_array on line 2.

/Fredrik
hominid4 21 Dec, 2010
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.
<?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!
GreyHead 22 Dec, 2010
Hi Wesley,

Apologies for my typo. The code goes in the OnSubmit Before Email box.

Bob
hominid4 22 Dec, 2010
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:
<?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 />";
?>
GreyHead 24 Dec, 2010
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:
<?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.
hominid4 25 Dec, 2010
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
GreyHead 25 Dec, 2010
Hi hominid4,

Oops, a little typo crept in there
$address[] = "$v : $k";
should be
$address[] = "$v : $temp";


Bob

PS I've corrected my original post.
hominid4 25 Dec, 2010
That's it! Thanks for all your time!!

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