Tom Tom - 7 months ago 13
PHP Question

Conditional PHP inside form email body

No need to be rude everyone. I really wasn't that far off, I just had my syntax wrong, as was politely indicated in the accepted answer. Isn't that the point of an open forum such as this? I have learned a lot on the site so thank you to those who are humble and not entitled. Original question below.




I have a form with many fields, but not all are required. I am no PHP wizard, but I wrote the following code (which doesn't work) to try to remove the empty fields from my form email.

$body = "
<h4>".$name." </h4>
<span'>is coming to ".$coming."</span>
<br/>
"if isset($name2){"
<h4'>".$name2." </h4>
<span'>is coming to ".$coming2."</span>
<br/>
"} ......... and so forth........


I realize this is poor logic, so have atter..

Answer

You're not properly concatenating the string. It should look like:

$body = "
<h4>".$name." </h4>
<span'>is coming to ".$coming."</span>
<br/>";

if (isset($name2)){
 //note the .= operator for appending (concatenating) to the string
  $body .="        
  <h4>".$name2." </h4>
  <span'>is coming to ".$coming2."</span>
  <br/>";
}