我有一个工作正常的电子邮件表单。我想添加一个功能,如果某些字段没有填充,它不会在电子邮件中将它们发送为空。如果没有填写这些字段,我怎么能不显示这些字段。这是我的完整代码。
$message = 'Hello,<br /><br />
First Name: '.$_POST['first_name'].'<br />
Last Name: '.$_POST['last_name'].'<br />
Email: '.$_POST['email'].'<br />
Address: '.$_POST['address'].'<br />
Phone: '.$_POST['phone'].'<br />
City: '.$_POST['city'].'<br />
State: '.$_POST['state'].'<br />
Zip Code: '.$_POST['zipcode'].'<br />
Country: '.$_POST['country'].'<br />
Preferred Method of Contact: '.$_POST['perferred'].'<br />
Comments/Questions:<br />
'.$_POST['description'].'<br /><br />
//This is the section I am speaking about
Interested in: <br />
'.$_POST['nh_home'].'<br />
'.$_POST['nh_lot'].'<br />
'.$_POST['nh_townhouse'].'<br />
'.$_POST['nh_condo'].'<br />
'.$_POST['nh_marina'].'<br />
'.$_POST['nh_beachclub'].'<br />
'.$_POST['nh_resale'].'<br /><br />
// End Section
How did you hear about us? <br />
'.$_POST['how_about'].'<br />
;
$to = 'email@domain.com';
$subject = 'Splash Page';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: Meredith Bay <info@website.com>' . "\r\n";
$send = mail($to, $subject, $message, $headers);
if($send){
header('Location: http://www.website.com/thank-you/');
exit;
}
else{
echo "error";
}
答案 0 :(得分:1)
据推测,您更完整的代码如下所示:
$email = 'some markup' . $_POST['nh_home'] . '<br />';
$email .= 'some markup' . $_POST['nh_lot'] . '<br />';
$email .= 'some markup' . $_POST['nh_townhouse'] . '<br />';
// etc...
您可以简单地在这些之间引入条件检查:
$email = '';
if ($_POST['nh_home']) {
$email .= 'some markup' . $_POST['nh_home'] . '<br />';
}
if ($_POST['nh_lot']) {
$email .= 'some markup' . $_POST['nh_lot'] . '<br />';
}
if ($_POST['nh_townhouse']) {
$email .= 'some markup' . $_POST['nh_townhouse'] . '<br />';
}
// etc...
答案 1 :(得分:0)
如果值不为空,请检查:
if ($_POST['nh_home']) echo 'Home: '.$_POST['nh_home'].'<br />';
if ($_POST['nh_lot']) echo 'Lot: '.$_POST['nh_lot'].'<br />';
if ($_POST['nh_townhouse']) echo 'Townhouse: '.$_POST['nh_townhouse'].'<br />';
if ($_POST['nh_condo']) echo 'Condo: '.$_POST['nh_condo'].'<br />';
if ($_POST['nh_marina']) echo 'Marina: '.$_POST['nh_marina'].'<br />';
if ($_POST['nh_beachclub']) echo 'Beachclub: '.$_POST['nh_beachclub'].'<br />';
if ($_POST['nh_resale']) echo 'Resale: '.$_POST['nh_resale'].'<br />';
答案 2 :(得分:0)
您可以使用
$message = 'Hello,<br /><br />';
if(empty($_POST['first_name'])) {
$message .= 'First Name: '.$_POST['first_name'].'<br />'
}
这只是一个基本条件。对POST
数据中的每个元素执行此操作。