You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP联系表单无法获取POST数据问题求助

联系表单POST数据无法获取问题排查与解决

问题描述

通过POST方法提交表单数据到assets/php/sent-mail.php,借助SendinBlue API发送邮件,但PHP文件无法读取$_POST数据,运行时输出array(0) { } Form fields are not set.,无法触发邮件发送逻辑,达不到“Your message has been sent. Thank you!”的预期效果。

表单代码

<form action="assets/php/sent-mail.php" method="post" role="form" class="email-form">
  <div class="row">
    <div class="col form-group">
      <input type="text" name="name" class="form-control" id="name"
        placeholder="Your Name" required>
    </div>
    <div class="col form-group">
      <input type="email" class="form-control" name="email" id="email"
        placeholder="Your Email" required>
    </div>
  </div>
  <div class="form-group">
    <input type="text" class="form-control" name="subject" id="subject"
      placeholder="Subject" required>
  </div>
  <div class="form-group">
    <textarea class="form-control" name="message" rows="5" placeholder="Message"
      required></textarea>
  </div>
  <div class="text-center"><button type="submit">Send Message</button></div>
</form>

PHP处理代码

<?php
require_once (__DIR__ . '/vendor/autoload.php');

use SendinBlue\Client\Configuration;
use SendinBlue\Client\Api\TransactionalEmailsApi;
use SendinBlue\Client\Model\SendSmtpEmail;

// Set up configuration
$config = Configuration::getDefaultConfiguration()->setApiKey('api-key', 'xxxxxxxx');

// Create an instance of the TransactionalEmailsApi
$apiInstance = new TransactionalEmailsApi(new GuzzleHttp\Client(), $config);

var_dump($_POST);

if (isset($_POST["name"], $_POST["email"], $_POST["subject"], $_POST["message"])) {
    // Form data
    $u_name = $_POST["name"];
    $u_email = $_POST["email"];
    $u_subject = $_POST["subject"];
    $u_message = $_POST["message"];

    // Build SendSmtpEmail object
    $emailObject = new SendSmtpEmail([
        'to' => [['email' => 'tapadippaul@gmail.com', 'name' => 'Tapadip']],
        'subject' => $u_subject,
        'htmlContent' => $u_message,
        'sender' => ['email' => $u_email, 'name' => $u_name],
    ]);

    try {
        // Send the email using the Sendinblue API
        $result = $apiInstance->sendTransacEmail($emailObject);

        // Redirect to a success page
        header("Location: /sent.html");
    } catch (Exception $e) {
        // Handle exceptions (you might want to log the error)
        echo 'Message could not be sent. Error: ', $e->getMessage();
    }
} else {
    // Handle the case when form fields are not set
    echo 'Form fields are not set.';
}

当前运行结果

  • array(0) { } Form fields are not set.

预期效果

  • 成功发送邮件后跳转到sent.html页面,显示“Your message has been sent. Thank you!”

排查与解决步骤

1. 验证表单提交路径

检查action属性中的assets/php/sent-mail.php是否为当前页面的正确相对路径。通过浏览器开发者工具的网络面板,查看表单提交请求的状态码:

  • 若返回404,说明路径错误,需修正为正确的文件路径;
  • 若返回200,继续排查其他原因。

2. 检查前端是否拦截表单提交

查看页面中是否存在JavaScript代码阻止了表单的默认提交行为(比如调用event.preventDefault()但未通过AJAX手动发送POST请求)。可以临时移除所有自定义JS代码,测试原生表单提交是否正常。

3. 确认PHP配置参数

检查服务器的php.ini文件:

  • 确保post_max_size值足够容纳表单数据(默认一般为8M,若表单内容较多可适当调整);
  • 确认max_input_vars数值足够(默认1000,若表单字段过多需调高);
  • 检查enable_post_data_reading是否为On(默认开启,禁用会导致$_POST为空)。

4. 检查表单enctype属性

当前表单无文件上传需求,不需要设置enctype属性。若误加了multipart/form-data且未在PHP中处理文件上传,会导致$_POST无法解析。确保表单标签中没有多余的enctype配置。

5. 调试原始请求数据

在PHP文件开头添加以下代码,查看原始POST请求内容:

var_dump(file_get_contents('php://input'));
  • 若能看到表单数据(如name=xxx&email=xxx...),说明$_POST解析异常,可能是content-type请求头不正确;
  • 若输出为空,说明请求未携带任何数据,问题出在前端提交环节。

6. 完善成功提示逻辑

当前代码成功发送邮件后会跳转至sent.html,需确保该页面存在并包含“Your message has been sent. Thank you!”的提示内容。若无需跳转,可修改PHP代码(注意在header()之前不能有任何输出,包括var_dump()):

// 移除var_dump($_POST);
try {
    $result = $apiInstance->sendTransacEmail($emailObject);
    echo 'Your message has been sent. Thank you!';
} catch (Exception $e) {
    echo 'Message could not be sent. Error: ', $e->getMessage();
}

内容的提问来源于stack exchange,提问作者DevOps.Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 10:57:33