PHP表单通过电子邮件发送,但$ _POST ['email']失败

时间:2019-03-07 05:15:13

标签: javascript php email post

我的网站上有一个表单,希望在提交时发送电子邮件。电子邮件被发送,但是没有内容随其一起发送。似乎isset($ _ POST ['email'])失败了。

这是我的表格:

<form id="sponsorForm" name="sponsor" role="form">
   <div class="modal-body">
      <div class="form-group col-md-12">
         <label for="sponsorname">Name</label>
         <input type="text" name="sponsorname" class="form-control">
      </div>
      <div class="form-group col-md-12">
         <label for="sponsoremail">Email</label>
         <input type="email" name="sponsoremail" class="form-control">
      </div>
      <div class="form-group col-md-12">
         <label for="sponsormessage">Message</label>
         <textarea class="form-control" name="sponsormessage" rows="7" placeholder="Message...">
         </textarea>
      </div>
   </div>
   <div class="modal-footer" style="border: none;">                  
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      <input type="submit" class="btn btn-success" data-toggle="modal" data-target="#sponsor-thanks" id="sponsor-submit">
   </div>
</form>

这里有一些JavaScript可以调用php:

$(document).ready(function(){   
    $("#sponsorForm").submit(function(event){
        submitSponsorForm();
        return false;
    });
});

function submitSponsorForm(){
     $.ajax({
        type: "POST",
        url: "sendSponsor.php",
        cache:false,
        data: $('form#sponsorForm').serialize(),
        success: function(response){
            $("#sponsor").html(response)
            $("#sponsor-modal").modal('hide');
        },
        error: function(){
            alert("Error");
        }
    });
}

这是php:

<?php
if (isset($_POST['email'])) {
    $sponsorname = strip_tags($_POST['sponsorname']);
    $sponsoremail = strip_tags($_POST['sponsoremail']);
   $sponsormessage = strip_tags($_POST['sponsormessage']);
    $message = "Name: ".$sponsorname."\r\nEmail: ".$sponsoremail."\r\nMessage: ".$sponsormessage; 
}
    mail("xxx@xxx.com", "subject", $message, "from: xxx");
?>

2 个答案:

答案 0 :(得分:2)

在您的表单中,电子邮件输入字段的名称为“ sponsoremail” 因此,您应该使用名称Sponsoremail作为$_POST变量的索引,作为$_POST['sponsoremail']的索引,以检查用户是否填写了电子邮件,但您使用了表单中未找到的$_POST['email']。 / p>

使用if (isset($_POST['sponsoremail']))代替if (isset($_POST['email'])),它应该可以工作。

答案 1 :(得分:1)

尝试

    <?php
    if (isset($_POST['sponsoremail'])) {
        $sponsorname = strip_tags($_POST['sponsorname']);
        $sponsoremail = strip_tags($_POST['sponsoremail']);
       $sponsormessage = strip_tags($_POST['sponsormessage']);
        $message = "Name: ".$sponsorname."\r\nEmail: ".$sponsoremail."\r\nMessage: ".$sponsormessage; 
      mail("xxx@xxx.com", "subject", $message, "from: xxx");
     }
    ?>