获取空白电子邮件

时间:2016-01-25 08:42:58

标签: javascript php email

我正在尝试创建一个不会重定向到另一个其他页面的电子邮件表单。我是PHP和Javascript的新手..所以基本上我编码像这样 -

我在mail.php文件中包含以下代码

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Message: $message";
$recipient = "devtest@xxx.org";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='test.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>

我已将所有文件上传到网络服务器,但问题是当我发送测试电子邮件时,我没有收到空白的电子邮件。

电子邮件仅包含发件人姓名和电子邮件ID。

请让我知道我搞砸了哪里。

<div class="contact">
  <div class="contact-wrap">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
    <script>
      function verify() {
        if (document.getElementById("name").value == "" || document.getElementById("email").value == "") {
          alert("Please enter a name and an email.");
        } else {
          alert("Looks good, sending email");
          //document.getElementById('ContactForm').submit();
          var name = $('#name').val();
          var email = $('#email').val();
          var formData = "name=" + name + "&email=" + email;
          $.ajax({
            url: "mail.php",
            type: "POST",
            data: formData,
            success: function (data, textStatus, jqXHR) {
              //data - response from server
              alert(data);
            },

          });
        }
      }
    </script>

    <div class="quick-contact">
      <form action="#" id="ContactForm" method="post" action="mail.php">

        <p>Your Name (required) <br>
          <input type="text" class="form-bt" value="" size="40" name="name" id="name" required>
        </p>

        <p>Your Email (required) <br>
          <input type="email" class="form-bt" value="" size="40" name="email" id="email" required>
        </p>

        <p>Subject <br>
          <input type="text" class="form-bt" value="" name="phone" size="40">
        </p>

        <p>Your Message<br>
          <textarea cols="40" rows="10" class="text-area" name="message" required></textarea>
        </p>

        <p>
          <input type="submit" id="send" onClick="verify()" value="Send" class="btton" onmouseover="this.style.backgroundColor = 'black'" ; onmouseout="this.style.backgroundColor = '#74b9e4'" ;>
        </p>

      </form>
    </div>
  </div>
</div>
亲切的问候 DEB

2 个答案:

答案 0 :(得分:1)

您刚刚发送了电子邮件和姓名。替换

var  formData = "name="+name+"&email="+email;

var  formData = "name="+name+"&email="+email+"&message="+message;

同时获取消息变量:

var message=$('#message').val();

将textarea更改为:

<textarea cols="40" rows="10" class="text-area" name="message" id="message" required></textarea>

答案 1 :(得分:1)

您发布的是仅包含姓名和电子邮件的表单数据。

var  formData = "name="+name+"&email="+email;

根据需要添加字段。 另外一个好方法是:

var formData = {
    name : $('#name').val(),
    email : $('#email').val(),
    message : $('#message').val() //Say ID for your message is "message"
}

而不是:

var  formData = "name="+name+"&email="+email+"&message="+message;