【问题标题】:How to prevent submitted empty fields from displaying on email form?如何防止提交的空字段显示在电子邮件表单上?
【发布时间】:2019-01-14 09:11:49
【问题描述】:

我有一个 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

交货日期:01/03/2019

发货时间:12:00

送货地址:测试员

电话:763601789

电子邮件:Test@yahoo.com

付款方式:万事达卡

如果填写了联系人字段,结果应如下所示:

* 一般信息 *

公司名称:Bl

联系人:Guy Tester

交货日期:01/03/2019

发货时间:12:00

送货地址:测试员

电话:763601789

特别说明:帮助

电子邮件:Test@yahoo.com

付款方式:万事达卡

目前结果如下:

* 一般信息 *

公司名称:测试

【问题讨论】:

    标签: php html forms email syntax


    【解决方案1】:

    您没有在条件语句之后构建字符串。而不是使用echo,您需要使用.= 运算符附加到字符串。换句话说,确保您将字符串分配并附加到$mail-&gt;Body 变量。您可能还想使用!empty($contact) 而不是!null($contact) :)

    <?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>';
    ?>
    

    【讨论】:

    • 部分;他们想检查空字段,每 如何防止提交的空字段显示在电子邮件表单上?”。就个人而言,我会使用 foreach 来对抗 empty() 或三元运算符。
    • @FunkFortyNiner 我肯定会使用三元运算符 :)
    • 谷歌搜索了一下(哈哈!)。 Here's one answer 我想到的。我以前用过类似的方法,效果很好。和this Q&A。但是三元组可能会更容易一些。编辑:但是foreach 在有很多输入时效果很好。
    • 没错。 foreach 带有我喜欢使用的键值来发送 HTML 格式的 HTML 表格中的内容,该表格将根据提交的内容自动填充,但这可能超出了问题的范围 :-) 但它确实服务我很好。 干杯
    • 我总是很想听听别人是怎么做的,虽然我知道这并不总是 cmets 的目的:) 总是很高兴聊天哈哈
    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 2015-04-22
    • 2017-08-12
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多