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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dburns
    New Member
    • Jan 2012
    • 3

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

    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:
    Code:
    $message = "Date: " . date("F d, Y") . " $n
    <table>
    <tr><th $headerStyle >Name</th><td>" . $this->AddName1 . "</td></tr>
    <tr><th $headerStyle >Name</th><td>" . $this->AddName2 . "</td></tr>
    <table>
    ";
    I tested unsuccessfully like this:
    Code:
    if (isset($_POST["AddName2"])) { 				
    <tr><th $headerStyle >Name</th><td>" . $this->AddName2 . "</td></tr>
    " . } . "
    Thanks in advance.
  • charles07
    New Member
    • Dec 2011
    • 45

    #2
    dburns

    use this code
    Code:
    <?php
    if($_POST["AddName2"] != '')
    {
    echo "<tr><td". $headerStyle." >Name</td><td>" . $_POST["AddName2"] . "</td>
    </tr>";
    }
    ?>

    Comment

    • dburns
      New Member
      • Jan 2012
      • 3

      #3
      Thank you but it appears the code enveloped in the $message variable is strictly defining what's allowed between the two dots:
      Code:
      " . if($_POST["AddName2"] != '') { 
      echo "<tr><td". $headerStyle." >Name</td><td>" . $_POST["AddName2"] . "</td></tr>" ; 
      } . "
      Per above, I still get syntax error: syntax error, unexpected T_IF
      Last edited by dburns; Jan 10 '12, 12:37 PM. Reason: Forgot to add.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        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).

        Comment

        • charles07
          New Member
          • Dec 2011
          • 45

          #5
          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"] != '') {

          Comment

          • dburns
            New Member
            • Jan 2012
            • 3

            #6
            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.

            Code:
                public function sendAdminNotification($email, $subject) {
                    
                    $n = "<br />";
                    
                    $headerStyle = 'style="font-weight: bold; background-color: #BCDAFC;"';
                    $message = "Date: " . date("F d, Y") . " $n
                    $n
                    $n Non Profit Org Registration Submitted. Information below.:
                    $n
                    <table cellpadding='3' colspacing='0'>
                        <tr><th $headerStyle >First Name</th><td>" . $this->first_name . "</td></tr>
                        <tr><th $headerStyle >Middle Initial</th><td>" . $this->middle_initial . "</td></tr>
                        <tr><th $headerStyle >Last Name</th><td>" . $this->last_name . "</td></tr>
                        <tr><th $headerStyle >Address</th><td>" . $this->address . "</td></tr>
                        <tr><th $headerStyle >Address 2</th><td>" . $this->address2 . "</td></tr>
                        <tr><th $headerStyle >City</th><td>" . $this->city . "</td></tr>
                        <tr><th $headerStyle >State</th><td>" . $this->state . "</td></tr>
                        <tr><th $headerStyle >Zip Code</th><td>" . $this->zip . "</td></tr>
                        <tr><th $headerStyle >Telephone</th><td>" . $this->telephone . "</td></tr>
                        <tr><th $headerStyle >Fax</th><td>" . $this->fax . "</td></tr>
                        <tr><th $headerStyle >Email</th><td>" . $this->email . "</td></tr>
                        <tr><th $headerStyle ># Participating</th><td>" . $this->num_individuals . "</td></tr>		
                        <tr><th $headerStyle >Name & Email #1</th><td>" . $this->AddName1 . "</td><td>" . $this->AddEmail1 . "</td></tr>
                        <tr><th $headerStyle >Name & Email #2</th><td>" . $this->AddName2 . "</td><td>" . $this->AddEmail2 . "</td></tr>			
                        <tr><th $headerStyle >Name & Email #3</th><td>" . $this->AddName3 . "</td><td>" . $this->AddEmail3 . "</td></tr>			
                        <tr><th $headerStyle >Name & Email #4</th><td>" . $this->AddName4 . "</td><td>" . $this->AddEmail4 . "</td></tr>			
                        <tr><th $headerStyle >Name & Email #5</th><td>" . $this->AddName5 . "</td><td>" . $this->AddEmail5 . "</td></tr>			
                    </table>
                    ";
            		
            		$this->_headers[] = 'From: Non Profit Contact'  . ' <noreply@NPO_test.org>';
            		//$this->_headers[] = 'NPO 1 Registration (mailto:noreply@NPO_test.org)';
            		
            		
                    
                    mail($email, $subject, $message, implode("\r\n", $this->_headers)) or die("Could not send admin email.");
                }

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              you would need to test the property for a null value and only then add the row to the table.

              Comment

              • charles07
                New Member
                • Dec 2011
                • 45

                #8
                hi dburns

                please try out the below code. u were missing some quote marks in between.
                Code:
                <?php
                public function sendAdminNotification($email, $subject) 
                                {
                 
                        $n = "<br />";
                 
                        $headerStyle = 'style="font-weight: bold; background-color: #BCDAFC;"';
                        $message = "Date: " . date("F d, Y") . $n;
                        $message .= $n." Non Profit Org Registration Submitted. Information below.:";
                        $message .= $n;
                		
                        $message .= "<table cellpadding='3' colspacing='0'>
                            <tr><th ".$headerStyle." >First Name</th><td>" . $this->first_name . "</td></tr>
                            <tr><th ".$headerStyle." >Middle Initial</th><td>" . $this->middle_initial . "</td></tr>
                            <tr><th ".$headerStyle." >Last Name</th><td>" . $this->last_name . "</td></tr>
                            <tr><th ".$headerStyle." >Address</th><td>" . $this->address . "</td></tr>
                            <tr><th ".$headerStyle." >Address 2</th><td>" . $this->address2 . "</td></tr>
                            <tr><th ".$headerStyle." >City</th><td>" . $this->city . "</td></tr>
                            <tr><th ".$headerStyle." >State</th><td>" . $this->state . "</td></tr>
                            <tr><th ".$headerStyle." >Zip Code</th><td>" . $this->zip . "</td></tr>
                            <tr><th ".$headerStyle." >Telephone</th><td>" . $this->telephone . "</td></tr>
                            <tr><th ".$headerStyle." >Fax</th><td>" . $this->fax . "</td></tr>
                            <tr><th ".$headerStyle." >Email</th><td>" . $this->email . "</td></tr>
                            <tr><th ".$headerStyle." ># Participating</th><td>" . $this->num_individuals . "</td></tr> ";
                            if($this->AddName1 != '')
                            {
                                $message .="<tr><th ".$headerStyle." >Name & Email #1</th><td>" . $this->AddName1 . "</td><td>" . $this->AddEmail1 . "</td></tr>";
                            }
                           if($this->AddName1 != '')
                            {
                                $message .="<tr><th ".$headerStyle." >Name & Email #2</th><td>" . $this->AddName2 . "</td><td>" . $this->AddEmail2 . "</td></tr>";
                            }
                            if($this->AddName1 != '')
                            {
                                $message .="<tr><th ".$headerStyle." >Name & Email #3</th><td>" . $this->AddName3 . "</td><td>" . $this->AddEmail3 . "</td></tr>";  
                            }
                            if($this->AddName1 != '')
                            {
                                $message .="<tr><th ".$headerStyle." >Name & Email #4</th><td>" . $this->AddName4 . "</td><td>" . $this->AddEmail4 . "</td></tr>"; 
                            }
                            if($this->AddName1 != '')
                            {
                                $message .="<tr><th ".$headerStyle." >Name & Email #5</th><td>" . $this->AddName5 . "</td><td>" . $this->AddEmail5 . "</td></tr>";
                            }
                            
                        $message .="</table> ";
                 
                        $this->_headers[] = 'From: Non Profit Contact'  . ' <noreply@NPO_test.org>';
                        //$this->_headers[] = 'NPO 1 Registration (mailto:noreply@NPO_test.org)';
                 
                 
                 
                        mail($email, $subject, $message, implode("\r\n", $this->_headers)) or die("Could not send admin email.");
                   }
                 ?>

                Comment

                Working...