Php $\u POST正在返回空数组

Php $\u POST正在返回空数组,php,html,post,Php,Html,Post,我今天浏览了很多论坛,想弄清楚为什么会发生这种情况。我已经安装了PHPMailer,一切正常。但是,在代码的顶部,您可以看到我试图定义使用$\u POST初始化的变量,例如,如果我运行var\u dump($\u POST['name']);它将为该值返回NULL。表单数据根本没有被发送过来。这可能是一个简单的错误,但如果你有洞察力,我们将不胜感激 PHP代码: <?php require 'PHPMailerAutoload.php'; $name = $_POS

我今天浏览了很多论坛,想弄清楚为什么会发生这种情况。我已经安装了PHPMailer,一切正常。但是,在代码的顶部,您可以看到我试图定义使用$\u POST初始化的变量,例如,如果我运行var\u dump($\u POST['name']);它将为该值返回NULL。表单数据根本没有被发送过来。这可能是一个简单的错误,但如果你有洞察力,我们将不胜感激

PHP代码:

       <?php
require 'PHPMailerAutoload.php';

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    print_r($_POST['name']);

    // if(isset($_POST['name'])) {
    //     $name = htmlspecialchars($_POST['name']);
    // }
    // if(isset($_POST['email'])) {
    //     $email = htmlspecialchars($_POST['email']);
    // }
    // if(isset($_POST['message'])) {
    //     $message = htmlspecialchars($_POST['message']);
    // }

    $mail = new PHPMailer();

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'xxxxxxxxxxxx';                 // SMTP username
    $mail->Password = 'xxxxxxxxxx';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    $mail->From = $email;
    $mail->FromName = $name;
    $mail->addAddress('xxxxxxxx');     // Add a recipient

    $mail->isHTML(true);                                  // Set email format to HTML

    $mail->Subject = "Contact Request Form From $name";
    $mail->Body = "
        <table width='450px'>
                <tr>
                    <td><strong>Name: </strong></td>
                    <td>$name</td>
                </tr>
                <tr>
                    <td><strong>Email: </strong></td>
                    <td>$email</td>
                </tr>
                <tr>
                    <td><strong>Message: </strong></td>
                    <td>$message</td>
                </tr>
        </table>
    ";

    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }

?>

您需要在此添加您的代码粘贴您的html和php代码。请从过程中完成php代码。php@AmitSharma发布了完整的PHP代码。@MagnusEriksson我道歉。我在使用var_dump($_POST['name'])时得到一个空值;我会适当地修改这个问题。
<div class="contact-form">
      <h3>Contact Me</h3>
      <p>Please use the form below to get in contact with me</p>
      <form action="process.php" method="post">
        <div class="form-group">
          <label for="name-1">Name</label>
          <input type="text" name="name" id="name-1" placeholder="Enter Name" class="style">
        </div>
        <div class="form-group">
          <label for="email-1">Email</label>
          <input type="email" name="email" id="email-1" placeholder="Enter Email" class="style">
        </div>
        <div class="form-group">
          <label for="message-1">Message</label>
          <textarea name="message" id="message-1" placeholder="Enter Message"></textarea>
        </div>
        <div>
          <input type="submit" value="Submit" class="btn confirm">
        </div>
      </form>
    </div>
// If submit button is clicked on contact form
document.querySelector('.confirm').onclick = function() {
  window.location.href = 'confirmed.html';
}