-2

I have an html form that is processed by a separate php file. In the php file, there is a section where an html confirmation email is sent to the person filling out the form. I will not post the entire confirmation email section, but here are the headers and the beginning of the message:

$pfw_subject = 'Confirmation of Registration for 28th Annual Insolvency Conference';
$pfw_email_to = "$Email,$AssistantEmail";
$pfw_header = "From: cbfadmin@jbsmgmt.com\n"
. "Bcc: cdrossen@cdrmarketing.com\n"
. "Reply-To: cbfadmin@jbsmgmt.com\n"
. "MIME-Version: 1.0\n"
. "Content-Type: text/html; charset=ISO-8859-1\n";
$pfw_message = "<html>
<head></head>
<body>.....continues

Here is the code of a table row that I am having a hard time with. There are 3 registration types (posted variable is $RegType and choices are Reg1, Reg2, and Reg3) with 3 different prices (posted variables are - $Total_Reg1, $Total_Reg2, $Total_Reg3. In the email I want it to read something like this:

Registration Type: $RegType - $Total_Reg1 Showing the registration type and the price

So I need to know which registration type the person selected in order to get the correct $ Total. Here is what I am attempting to do, but it is not working:

    <tr>
      <td width='35%' height='18'>Registration Type:</td>
      <td width='65%' height='18'>"
      if($RegType == "Reg1"){"
      $RegType - $Total_Reg1"
      else if ($RegType == "Reg2"){"
      $RegType - $Total_Reg2"
      else if ($RegType == "Reg3"){"
      $RegType - $Total_Reg3"
      }"
      </td>
    </tr>

    ....html continues, then finally:

   </body>
   </html>";

What is happening is the email prints out but stops at Registration Type: and does not give the value and also aborts the rest of the email. Probably some sort of syntax error and I'm sure there is a better way to do this so any help would be greatly appreciated. Thanks in advance.

cdr6800
  • 105
  • 2
  • 9
  • 1
    hard to say if you're injecting PHP into HTML without PHP tags here. use error reporting http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Oct 25 '15 at 01:05
  • If you can, step into that page with a debugger and check what value is in $RegType. If you are not able to debug, just output what $RegType is there. It is probably either not set, or contains a value you don't expect. If the page were not being processed as PHP, you would probably see your code in the email itself so that is likely not the issue. – Eric J. Oct 25 '15 at 01:07

2 Answers2

1

You're absolutely right, there's definitely some syntax issues going on here. You're defining your HTML in a string, but you can't just abitrarily throw an if statement into the middle of it. You have to format your PHP properly; you need to terminate the string and add onto it. Also, you're missing the end braces on your if statements (I've actually just removed them, because they're 1-liners).

This is generally not a good way to write HTML because it makes it very difficult to read. Regardless...

$pfw_message = "<html><head></head><body><table><tr><td width='35%' height='18'>Registration Type:</td><td width='65%' height='18'>";
if ( $RegType == "Reg1" )
  $pfw_message .= $RegType . ' - ' . $Total_Reg1;
else if ( $RegType == "Reg2" )
  $pfw_message .= $RegType . ' - ' . $Total_Reg2;
else if ( $RegType == "Reg3" )
  $pfw_message .= $RegType . ' - ' . $Total_Reg3;
$pfw_message .= "</td></tr></table></body></html>";

A better method, if doing something like this, involves the use of output buffering. You go back to writing your HTML as HTML, and inject the PHP where required.

<?php

// stuff

ob_start(); ?>

<html>
  <head></head>
  <body>
    <table>
      <tr>
        <td width='35%' height='18'>Registration Type:</td>
        <td width='65%' height='18'>
          <?php
          if ( $RegType == "Reg1" )
            echo $RegType . ' - ' . $Total_Reg1;
          else if ( $RegType == "Reg2" )
            echo $RegType . ' - ' . $Total_Reg2;
          else if ( $RegType == "Reg3" )
            echo $RegType . ' - ' . $Total_Reg3;
          ?>
        </td>
      </tr>
    </table>
  </body>
</html>

<?php 
$pfw_message = ob_get_clean();
// more stuff
Christian
  • 19,605
  • 3
  • 54
  • 70
0

Try This

$pfw_message = ' <html>
<head></head>
<body>
<tr>
  <td width=\'35%\' height=\'18\'>Registration Type:</td>
  <td width=\'65%\' height=\'18\'>"
  if($RegType == "Reg1"){"
  $RegType - $Total_Reg1"
  else if ($RegType == "Reg2"){"
  $RegType - $Total_Reg2"
  else if ($RegType == "Reg3"){"
  $RegType - $Total_Reg3"
  }"
  </td>
</tr>
</body>
</html>';
chris85
  • 23,846
  • 7
  • 34
  • 51