By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

Email headers $message - Don't Show if not exists

P: 3
Somewhat new to Php. I inherited a simple piece of email processing code that stores the submitted form fields in the $message variable. Their are some fields that may not exist when the form is submitted so I'd like to not include them.

The second row (AddName2) is an example of a row I don't want to include in the email submission if it doesn't exist:
Expand|Select|Wrap|Line Numbers
  1. $message = "Date: " . date("F d, Y") . " $n
  2. <table>
  3. <tr><th $headerStyle >Name</th><td>" . $this->AddName1 . "</td></tr>
  4. <tr><th $headerStyle >Name</th><td>" . $this->AddName2 . "</td></tr>
  5. <table>
  6. ";
I tested unsuccessfully like this:
Expand|Select|Wrap|Line Numbers
  1. if (isset($_POST["AddName2"])) {                 
  2. <tr><th $headerStyle >Name</th><td>" . $this->AddName2 . "</td></tr>
  3. " . } . "
  4.  
Thanks in advance.
Jan 10 '12 #1
Share this Question
Share on Google+
7 Replies


P: 45
dburns

use this code
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if($_POST["AddName2"] != '')
  3. {
  4. echo "<tr><td". $headerStyle." >Name</td><td>" . $_POST["AddName2"] . "</td>
  5. </tr>";
  6. }
  7. ?>
  8.  
Jan 10 '12 #2

P: 3
Thank you but it appears the code enveloped in the $message variable is strictly defining what's allowed between the two dots:
Expand|Select|Wrap|Line Numbers
  1. " . if($_POST["AddName2"] != '') { 
  2. echo "<tr><td". $headerStyle." >Name</td><td>" . $_POST["AddName2"] . "</td></tr>" ; 
  3. } . "
  4.  
Per above, I still get syntax error: syntax error, unexpected T_IF
Jan 10 '12 #3

Dormilich
Expert Mod 5K+
P: 8,639
Per above, I still get syntax error: syntax error, unexpected T_IF
of course. an if statement is not a string (which you would need when using the concatenation operator).
Jan 10 '12 #4

P: 45
dburns

i can't make out what you say. Could u send the entire code including your PHP tags.
Also why do you add dot before if condition

" . if($_POST["AddName2"] != '') {
Jan 11 '12 #5

P: 3
Thank you again for your followup. Here's the code, which contains "Name & Email" rows I would like to 'not include' if they're not filled out.

Expand|Select|Wrap|Line Numbers
  1.     public function sendAdminNotification($email, $subject) {
  2.  
  3.         $n = "<br />";
  4.  
  5.         $headerStyle = 'style="font-weight: bold; background-color: #BCDAFC;"';
  6.         $message = "Date: " . date("F d, Y") . " $n
  7.         $n
  8.         $n Non Profit Org Registration Submitted. Information below.:
  9.         $n
  10.         <table cellpadding='3' colspacing='0'>
  11.             <tr><th $headerStyle >First Name</th><td>" . $this->first_name . "</td></tr>
  12.             <tr><th $headerStyle >Middle Initial</th><td>" . $this->middle_initial . "</td></tr>
  13.             <tr><th $headerStyle >Last Name</th><td>" . $this->last_name . "</td></tr>
  14.             <tr><th $headerStyle >Address</th><td>" . $this->address . "</td></tr>
  15.             <tr><th $headerStyle >Address 2</th><td>" . $this->address2 . "</td></tr>
  16.             <tr><th $headerStyle >City</th><td>" . $this->city . "</td></tr>
  17.             <tr><th $headerStyle >State</th><td>" . $this->state . "</td></tr>
  18.             <tr><th $headerStyle >Zip Code</th><td>" . $this->zip . "</td></tr>
  19.             <tr><th $headerStyle >Telephone</th><td>" . $this->telephone . "</td></tr>
  20.             <tr><th $headerStyle >Fax</th><td>" . $this->fax . "</td></tr>
  21.             <tr><th $headerStyle >Email</th><td>" . $this->email . "</td></tr>
  22.             <tr><th $headerStyle ># Participating</th><td>" . $this->num_individuals . "</td></tr>        
  23.             <tr><th $headerStyle >Name & Email #1</th><td>" . $this->AddName1 . "</td><td>" . $this->AddEmail1 . "</td></tr>
  24.             <tr><th $headerStyle >Name & Email #2</th><td>" . $this->AddName2 . "</td><td>" . $this->AddEmail2 . "</td></tr>            
  25.             <tr><th $headerStyle >Name & Email #3</th><td>" . $this->AddName3 . "</td><td>" . $this->AddEmail3 . "</td></tr>            
  26.             <tr><th $headerStyle >Name & Email #4</th><td>" . $this->AddName4 . "</td><td>" . $this->AddEmail4 . "</td></tr>            
  27.             <tr><th $headerStyle >Name & Email #5</th><td>" . $this->AddName5 . "</td><td>" . $this->AddEmail5 . "</td></tr>            
  28.         </table>
  29.         ";
  30.  
  31.         $this->_headers[] = 'From: Non Profit Contact'  . ' <noreply@NPO_test.org>';
  32.         //$this->_headers[] = 'NPO 1 Registration (mailto:noreply@NPO_test.org)';
  33.  
  34.  
  35.  
  36.         mail($email, $subject, $message, implode("\r\n", $this->_headers)) or die("Could not send admin email.");
  37.     }
  38.  
Jan 11 '12 #6

Dormilich
Expert Mod 5K+
P: 8,639
you would need to test the property for a null value and only then add the row to the table.
Jan 11 '12 #7

P: 45
hi dburns

please try out the below code. u were missing some quote marks in between.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. public function sendAdminNotification($email, $subject) 
  3.                 {
  4.  
  5.         $n = "<br />";
  6.  
  7.         $headerStyle = 'style="font-weight: bold; background-color: #BCDAFC;"';
  8.         $message = "Date: " . date("F d, Y") . $n;
  9.         $message .= $n." Non Profit Org Registration Submitted. Information below.:";
  10.         $message .= $n;
  11.  
  12.         $message .= "<table cellpadding='3' colspacing='0'>
  13.             <tr><th ".$headerStyle." >First Name</th><td>" . $this->first_name . "</td></tr>
  14.             <tr><th ".$headerStyle." >Middle Initial</th><td>" . $this->middle_initial . "</td></tr>
  15.             <tr><th ".$headerStyle." >Last Name</th><td>" . $this->last_name . "</td></tr>
  16.             <tr><th ".$headerStyle." >Address</th><td>" . $this->address . "</td></tr>
  17.             <tr><th ".$headerStyle." >Address 2</th><td>" . $this->address2 . "</td></tr>
  18.             <tr><th ".$headerStyle." >City</th><td>" . $this->city . "</td></tr>
  19.             <tr><th ".$headerStyle." >State</th><td>" . $this->state . "</td></tr>
  20.             <tr><th ".$headerStyle." >Zip Code</th><td>" . $this->zip . "</td></tr>
  21.             <tr><th ".$headerStyle." >Telephone</th><td>" . $this->telephone . "</td></tr>
  22.             <tr><th ".$headerStyle." >Fax</th><td>" . $this->fax . "</td></tr>
  23.             <tr><th ".$headerStyle." >Email</th><td>" . $this->email . "</td></tr>
  24.             <tr><th ".$headerStyle." ># Participating</th><td>" . $this->num_individuals . "</td></tr> ";
  25.             if($this->AddName1 != '')
  26.             {
  27.                 $message .="<tr><th ".$headerStyle." >Name & Email #1</th><td>" . $this->AddName1 . "</td><td>" . $this->AddEmail1 . "</td></tr>";
  28.             }
  29.            if($this->AddName1 != '')
  30.             {
  31.                 $message .="<tr><th ".$headerStyle." >Name & Email #2</th><td>" . $this->AddName2 . "</td><td>" . $this->AddEmail2 . "</td></tr>";
  32.             }
  33.             if($this->AddName1 != '')
  34.             {
  35.                 $message .="<tr><th ".$headerStyle." >Name & Email #3</th><td>" . $this->AddName3 . "</td><td>" . $this->AddEmail3 . "</td></tr>";  
  36.             }
  37.             if($this->AddName1 != '')
  38.             {
  39.                 $message .="<tr><th ".$headerStyle." >Name & Email #4</th><td>" . $this->AddName4 . "</td><td>" . $this->AddEmail4 . "</td></tr>"; 
  40.             }
  41.             if($this->AddName1 != '')
  42.             {
  43.                 $message .="<tr><th ".$headerStyle." >Name & Email #5</th><td>" . $this->AddName5 . "</td><td>" . $this->AddEmail5 . "</td></tr>";
  44.             }
  45.  
  46.         $message .="</table> ";
  47.  
  48.         $this->_headers[] = 'From: Non Profit Contact'  . ' <noreply@NPO_test.org>';
  49.         //$this->_headers[] = 'NPO 1 Registration (mailto:noreply@NPO_test.org)';
  50.  
  51.  
  52.  
  53.         mail($email, $subject, $message, implode("\r\n", $this->_headers)) or die("Could not send admin email.");
  54.    }
  55.  ?>
  56.  
Jan 12 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.