【问题标题】:PHP Mail Form, labeling the $_POST in the $messagePHP 邮件表单,在 $message 中标记 $_POST
【发布时间】:2018-09-07 13:41:40
【问题描述】:

这是我现在使用的代码,效果很好。

<?php 
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
};
    $from = "someone@there.com";
    $to = "someone@here.com";
    $subject = "Customer Order";
    $message = "{$_POST['name']}
{$_POST['message']}
{$_POST['item1']} -Item 1
{$_POST['item2']} -Item 2";
    $headers = "From:" . $from;
    mail($to,$subject,$message, $headers);
    header('Location: thank-you1.html');


?>

当我收到发送的电子邮件时,无论输入了“item1”还是“item2”的数量,电子邮件都会显示 -项目 1 -项目2

如何排列这些标签,以便它们仅在该数量框有值时出现。

【问题讨论】:

    标签: php email post message identify


    【解决方案1】:

    在将它们添加到正在撰写的电子邮件之前,请查看表单中的项目变量是否已设置。

    <?php
    if(!isset($_POST['submit']))
    {
        //This page should not be accessed directly. Need to submit the form.
        echo "error; you need to submit the form!";
    };
        $from = "someone@there.com";
        $to = "someone@here.com";
        $subject = "Customer Order";
        $message = $_POST['name'] .
                   $_POST['message'];
    
        if (isset($_POST['item1'])) {
            $message .= $_POST['item1'];
        }
        if (isset($_POST['item2']) {
            $message .= $_POST['item2'];
        }
        $headers = "From:" . $from;
        mail($to,$subject,$message, $headers);
        header('Location: thank-you1.html');
    

    【讨论】:

      【解决方案2】:

      您可以更改以下格式的 $message

      $message = "{$_POST['name']}";
      $message .= "{$_POST['message']}";
      if(isset($_POST['item1']) & !empty($_POST['item1'])) {
      $message .= "{$_POST['item1'] -Item 1}";
      }
      if(isset($_POST['item2']) & !empty($_POST['item2'])) {
      $message .= "{$_POST['item2'] -Item 2}";
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-28
        • 1970-01-01
        • 2012-03-23
        • 1970-01-01
        • 2014-01-12
        • 1970-01-01
        • 2014-11-14
        • 1970-01-01
        相关资源
        最近更新 更多