Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何防止提交的空字段显示在电子邮件表单上?_Php_Html_Forms_Email_Syntax - Fatal编程技术网

Php 如何防止提交的空字段显示在电子邮件表单上?

Php 如何防止提交的空字段显示在电子邮件表单上?,php,html,forms,email,syntax,Php,Html,Forms,Email,Syntax,我有一个HTML表单,当提交时,它通过PHP电子邮件处理程序。除了在发送电子邮件时显示所有字段(包括空字段)外,其他一切都正常工作。我希望它只显示填写的字段 我已经设置了一个IF语句来检查变量!null显示在电子邮件中,如果为null,则不显示。它在一定程度上起作用。它会阻止在电子邮件中显示它遇到的第一个空字段,但随后会阻止显示其他有效字段。我相信这是我的语法错误 <?php $company_name = $_REQUEST['company_name']; $contact = $_R

我有一个HTML表单,当提交时,它通过PHP电子邮件处理程序。除了在发送电子邮件时显示所有字段(包括空字段)外,其他一切都正常工作。我希望它只显示填写的字段

我已经设置了一个IF语句来检查变量!null显示在电子邮件中,如果为null,则不显示。它在一定程度上起作用。它会阻止在电子邮件中显示它遇到的第一个空字段,但随后会阻止显示其他有效字段。我相信这是我的语法错误

<?php
$company_name = $_REQUEST['company_name'];
$contact = $_REQUEST['contact'];
$delivery_date = $_REQUEST['delivery_date'];
$delivery_time = $_REQUEST['delivery_time'];
$delivery_address = $_REQUEST['delivery_address'];
$phone = $_REQUEST['phone'];
$special_instruction = $_REQUEST['special_instruction'];
$email = $_REQUEST['email'];
$payment = $_REQUEST['payment'];

$mail->Body    = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
  <p>
    <strong>Company Name:</strong> <span style="color:red;">'.$company_name.'</span><br />';
    if(!is_null($contact)){echo'<strong>Contact:</strong> <span style="color:red;">'.$contact.'</span><br />';};
    '<strong>Delivery Date:</strong> <span style="color:red;">'.$delivery_date.'</span><br />
    <strong>Delivery Time:</strong> <span style="color:red;">'.$delivery_time.'</span><br />
    <strong>Delivery Address:</strong> <span style="color:red;">'.$delivery_address.'</span><br />
    <strong>Phone:</strong> <span style="color:red;">'.$phone.'</span><br />
    <strong>Special Instruction:</strong> <span style="color:red;">'.$special_instruction.'</span><br />
    <strong>Email:</strong> <span style="color:red;">'.$email.'</span><br />
    <strong>Type of Payment:</strong> <span style="color:red;">'.$payment.'</span><br />
  </p>
</body>
</html>';
?>

有时会填写“联系人”字段,但并非总是如此。我试图将PHP设置为仅当字段中有提交的数据时才显示信息。如果Contact字段没有数据,则结果应如下所示:

*一般信息*

公司名称:Bl

交付日期:2019年3月1日

交货时间:12:00

送货地址:测试员

电话:763601789

电邮:Test@yahoo.com

付款方式:万事达卡

如果填写了Contact字段,则结果应如下所示:

*一般信息*

公司名称:Bl

联系人:Guy测试员

交付日期:2019年3月1日

交货时间:12:00

送货地址:测试员

电话:763601789

特别说明:帮助

电邮:Test@yahoo.com

付款方式:万事达卡

目前的结果如下所示:

*一般信息*


公司名称:Test

您没有在条件语句之后构建字符串。您需要使用
=
运算符将其附加到字符串,而不是使用
echo
。换句话说,确保您正在将字符串赋值并附加到
$mail->Body
变量。您可能想使用
!空($contact)
而不是
!空($contact)
也:)



部分;他们想根据“如何防止提交的空字段显示在电子邮件表单上?”检查空字段。就我个人而言,我会用一个
foreach
来对付
empty()
或三元运算符。@FunkFortyNiner我肯定会用三元运算符:)谷歌搜索了一下(哈哈!)。我心里想的。我以前也用过类似的方法,效果很好。和。但三元可能更容易。编辑:但是当有很多输入时,
foreach
非常有效。没错。带有键值的
foreach
,我喜欢用来发送HTML表格中的HTML格式的东西,该表格将根据提交的内容自动填充自身,但这可能超出了问题的范围:-),但它确实很适合我。干杯我总是很想听听别人是怎么做的,虽然我知道这不是评论的目的:)总是很高兴聊天哈哈
<?php
$mail->Body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
  <p>
    <strong>Company Name:</strong> <span style="color:red;">'.$company_name.'</span><br />';
    if (!empty($contact))
    {
        $mail->Body .= '<strong>Contact:</strong> <span style="color:red;">'.$contact.'</span><br />';
    }
    $mail->Body .= '<strong>Delivery Date:</strong> <span style="color:red;">'.$delivery_date.'</span><br />
    <strong>Delivery Time:</strong> <span style="color:red;">'.$delivery_time.'</span><br />
    <strong>Delivery Address:</strong> <span style="color:red;">'.$delivery_address.'</span><br />
    <strong>Phone:</strong> <span style="color:red;">'.$phone.'</span><br />
    <strong>Special Instruction:</strong> <span style="color:red;">'.$special_instruction.'</span><br />
    <strong>Email:</strong> <span style="color:red;">'.$email.'</span><br />
    <strong>Type of Payment:</strong> <span style="color:red;">'.$payment.'</span><br />
  </p>
</body>
</html>';
?>