开发者

How to add a simple IF statement in this PHP file?

开发者 https://www.devze.com 2023-01-05 09:10 出处:网络
Question by a novice, here: I have a simple form returning a person\'s name, company, and address among other things.

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['o开发者_开发知识库ptin'];
$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!


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)));


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.


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);


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消