【问题标题】:What can make the $_POST variable parse as empty whilw using PHPMailer to send the email? [closed]$POST 变量为空。使用 PHPMailer 发送电子邮件 [关闭]
【发布时间】:2022-10-17 20:54:21
【问题描述】:

我有一个简单的表格:

<form method="post" action="/inc/contact.php"   id="contact-form" >         
       <div class="messages"></div>
       <div>
      <div>
        <div>
          <label>Name*</label>
          <input type="text" placeholder="John Doe" name="name"
       required="required" data-error="Name is required."> 
          <div ></div>
        </div>
       </div>

       <div>
        <div >
          <label>Email*</label>
          <input type="email" placeholder="john.doe@mybusiness.com" 
        name="email" required="required" data-error="Valid email is          
    required.">
          <div class="help-block with-errors"></div>
        </div>
      </div>
      <div>
        <div >
          <textarea placeholder="Your message*" name="message" 
     required="required" data-error="Please,leave us a message."></textarea>
          <div></div>
        </div>
      </div>
      <div ><button type="submit" name="submit">Send Message</button></div>
      </div>
      </form>

它将数据发送到contact.php 文件。我正在使用 MailerPHP 发送电子邮件。邮件工作正常我已经验证了这一点

 if(empty($_POST['submit'])) {

陈述。 如果我在 If 语句中包含所有内容,则不会发送任何内容。 看起来 $_POST 变量确实是空的。 我关注了各种帖子,似乎都没有解决我的问题。

    <?php
/**
 * This example shows making an SMTP connection with authentication.
 */

 use PHPMailer\PHPMailer\PHPMailer;
 use PHPMailer\PHPMailer\Exception;

 require 'phpmailer/src/Exception.php';
 require 'phpmailer/src/PHPMailer.php';
 require 'phpmailer/src/SMTP.php';
$_POST = json_decode(file_get_contents('php://input'), true);

  if(!empty($_POST['submit'])) {

    $mail = new PHPMailer(true);

    $mail->SMTPDebug = 0;

    $mail->Host = 'host';
    $mail->SMTPAuth = true;
    //Username to use for SMTP authentication
    $mail->Username = 'uname';
    //Password to use for SMTP authentication
    $mail->Password = 'pass';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    //Set who the message is to be sent from
    $mail->setFrom('xxx@example.com', 'First Last');
    //Set an alternative reply-to address
    $mail->addReplyTo('xxx@example.com', 'First Last');
    //Set who the message is to be sent to
    $mail->addAddress('xxx@example.com', 'John Doe');

    $mail->isHTML(true);
    $mail->Subject = 'Subject';
    $mail->Body .= "this is a message" . $_POST['message'] ;

    try {
        $mail->send();
        echo 'Your message was sent successfully!';
    } catch (Exception $e) {
        echo "Your message could not be sent!";
      
    }

    } else {
    echo "There is a problem with the contact.html
    document!";             
    }

资料来源: Post 1

Post 2

Post 3

我的开发人员工具的网络选项卡的响应看起来已填充

{
    "name": "sdsdsd",
    "email": "xxx@example.com",
    "message": "kjlhjkuhkkhm",
    "submit": ""
}

什么可以使 $_POST 变量解析为空?

编辑:这个答案的解决方案来自几个答案:

  1. 这段代码正在清空 $_POST 变量

    $_POST = json_decode(file_get_contents('php://input'),   true);
    1. 我必须向我的提交按钮添加值 值="发送"
    2. 我还必须通过添加到我的代码来验证我的变量是否包含一个值: var_dump($_POST)
    3. 总体而言,我遵循了其他用户建议的调试步骤,这让我对我正在寻求的解决方案充满信心。

    我打算将此问题用作我项目下一阶段的垫脚石,因此我认为这是必要的存在并且不会被删除。

【问题讨论】:

  • the form request is populated with data.... 是的,但是正如您所看到的,提交参数没有值(因为您没有在 html 中设置一个)...它是空的。尝试给它一个值,或者删除空测试 - 检查它是使用isset() 设置的应该就足够了。
  • 好的,那么实际上还有其他事情发生,因为这一切看起来都是正确的。确保您没有任何其他表单或外部按钮尝试提交。在提交页面之前打开浏览器的开发者控制台,然后在提交时查看“网络”选项卡。如果单击请求,是否看到请求参数?是否有任何重定向发生?
  • 您是否用json_decode() 覆盖$_POST 超全局?表单是如何提交的?
  • 该表单不会发送 JSON 编码的有效负载,它将是表单提交,$_POST 将被自动填充。您发布的代码覆盖了$_POST 超全局,但json_decode() 给出了null,因为它无法解析(它不是JSON 数据)。
  • @neuticle 你能建议一个解决方案或解释为什么这个表格会发生这种情况吗?我的意思是这是一个超级简单的联系表格。

标签: php html forms


【解决方案1】:

@neuticle 的回答解决了这个问题

删除行:

$_POST =json_decode(file_get_contents('php://input'), true);  

完全地。这是不必要的并覆盖了 $_POST 超全局"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2013-10-05
    相关资源
    最近更新 更多