0

Question by a novice, here:

I have a simple form returning a person's name, company, and address among other things.

Right now, these are returned on separate lines. If there is no entry in the "company" field, there is a blank line in the email that is sent.

I'd like to make an IF statement so that if there is no entry in the "company" field, that "address" will come back right after "name" without an extra space, but will include the "company" info if that field is filled-in.

The relevant part of the PHP code looks like this:

$name = $_POST['name'];
$company = $_POST['company'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$optin = $_POST['optin'];
$comments = ($_POST['comments']);



$body = <<<EOD

Please send samples to:

$name
$company
$address
$city, $state $zip

Email: $email

Opt-In to occasional email list?: $optin

Comments: $comments

EOD;
$headers = "From: $email\r\n";
$success = mail($webMaster, $emailSubject, $body,
$headers);

I will really appreciate your help!

4 Answers 4

2

You'll have to break out of HEREDOC for this, or prepare the value beforehand, but this is nice and short:

echo join("\n", array_filter(array($name, $company, $address)));
Sign up to request clarification or add additional context in comments.

Comments

0

What about this?

$name = $_POST['name'];
//...

if(empty($company)){
  $nameAndCompany = $name;
}else{
  $nameAndCompany = $name."\n".$company;
}
// or using the ternary operator, because it's a rather simple if
$nameAndCompany = (empty($company))?$name:$name."\n".$company;
$body = <<<EOD
...

$nameAndCompany
$address
$city, $state $zip
...

\n in double quotes means newline. See the php.net documentation for more info on the ternary operator.

2 Comments

the array_filter trick is still the cleanest solution (no IF...)
I'm glad I could help. @Yanick: Why is an "IF" not clean?
0

You are using heredoc syntax in your PHP script. You cannot use IF statements in there. But you can use other methods :

  • close your PHP tag, and use <?= $var ?> in your code
  • use templates and replace keywords with preg_replace(), etc.
  • put all your strings into an array and use implode("\n", $arr) to construct a nice string out of it
  • concatenate your string directly

The easiest way is the first one (EDITED with array_filter from another answer):

$name = $_POST['name'];
$company = $_POST['company'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$optin = $_POST['optin'];
$comments = ($_POST['comments']);



ob_start();
?>
Please send samples to:

<?= implode("\n", array_filter($name, $company, $address)) ?>
<?= $city ?>, <?= $state ?> <?= $zip ?>

Email: <?= $email ?>

Opt-In to occasional email list?: <?= $optin ?>

Comments: <?= $comments ?>

?>
$body = ob_get_clean();  // return the output since ob_start()

$headers = "From: $email\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

3 Comments

Actually, the OP is already using kind of a template: a heredoc string. I'd say that using the output buffer is a rather quirky way to achieve what the OP wants to do. I like the array way you suggested, though.
yes, I use arrays personally. Pretty useful when you got many embedded IF's and need to construct output with them...
But, I tried the IF statements and they did work. Both of Middus' solutions worked. Thank you all for so much help!!!!!
0

I edited this to use \n instead of <br />

$optin = $_POST['optin'];
$comments = ($_POST['comments']);



$body = "Please send samples to:";
if($_POST['name'] != '') $body .= "\n".$_POST['name'];
if($_POST['company'] != '') $body .= "\n".$_POST['company'];
if($_POST['address'] != '') $body .= "\n".$_POST['address'];
if($_POST['city'] != '') $body .= "\n".$_POST['city'];
if($_POST['state'] != '') $body .= ", ".$_POST['state'];
if($_POST['zip'] != '') $body .= " ".$_POST['zip'];
if($_POST['email'] != '') $body .= "\n\nEmail: ".$_POST['email'];
$body .= "Opt-In to occasional email list?: $optin";
$body .= "\n\nComments: $comments";


$headers = "From: $email\r\n";
$success = mail($webMaster, $emailSubject, $body,
$headers);

3 Comments

you'll want to use "\n"s instead of "<br />"s: his script uses carriage returns not HTML linebreaks
He does not seem to want HTML breaks.
"\n"s sound great! I had HTML, but was talked out of it earlier today. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.