$ _POST返回空数组

时间:2019-12-18 11:47:28

标签: php html post

我今天已经浏览了很多论坛,以弄清楚为什么会这样。我已经设置了PHPMailer,并且一切正常。但是,在代码的顶部,您可以看到我尝试定义以$ _POST初始化的变量,但是例如,如果我运行var_dump($ _ 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表单代码:

<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>

JavaScript代码:

// If submit button is clicked on contact form
document.querySelector('.confirm').onclick = function() {
  window.location.href = 'confirmed.html';
}

0 个答案:

没有答案