AJAX没有使用$ _POST发送任何信息

时间:2014-01-07 14:57:02

标签: php ajax fancybox

我按照fancybox教程和联系表单的AJAX查询。我唯一改变的是.php文件的位置,其中包含所有sendmail信息。当您填写联系表单时,电子邮件会发送,但根本没有数据。

以下是表格:

<div id="inline">
<h1>Send us a Message</h1>
<hr>

<form id="contact" name="contact" action="#" method="post">
    <label for="email">Your E-mail</label>
    <input type="email" id="email" name="email" class="txt">
    <br>
    <label for="msg">Enter a Message</label>
    <textarea id="msg" name="msg" class="txtarea"></textarea>

    <button id="send">Send E-mail</button>
</form>
</div>

和AJAX代码;

<script type="text/javascript">
function validateEmail(email) { 
    var reg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|    (\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return reg.test(email);
}

$(document).ready(function() {
    $(".modalbox").fancybox();
    $("#contact").submit(function() { return false; });


    $("#send").on("click", function(){
        var emailval  = $("#email").val();
        var msgval    = $("#msg").val();
        var msglen    = msgval.length;
        var mailvalid = validateEmail(emailval);

        if(mailvalid == false) {
            $("#email").addClass("error");
        }
        else if(mailvalid == true){
            $("#email").removeClass("error");
        }

        if(msglen < 4) {
            $("#msg").addClass("error");
        }
        else if(msglen >= 4){
            $("#msg").removeClass("error");
        }

        if(mailvalid == true && msglen >= 4) {

            $("#send").replaceWith("<em>sending...</em>");

            $.ajax({
                type: 'POST',
                url: 'contactforms/send_gm.php',
                data: $("#contact").serialize(),
                success: function(data) {
                    if(data == "true") {
                        $("#contact").fadeOut("fast", function(){
                            $(this).before("<p><strong>Subspace message has been transmitted successfully.</strong></p>");
                                setTimeout("$.fancybox.close()", 2000);
                        });
                    }
                }
            });
        }
    });
});
</script>

send_gm.php页面就是这个;

<?php
$sendto   = "blah@blah.com";
$usermail = $_POST['email'];
$content  = nl2br($_POST['msg']);

$subject  = "Contact Us: Game Management Query";
$headers  = "From: " . strip_tags($usermail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($content) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";

$msg  = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2>New User Feedback</h2>\r\n";
$msg .= "<p><strong>Sent by:</strong> ".$usermail."</p>\r\n";
$msg .= "<p><strong>Message:</strong> ".$content."</p>\r\n";
$msg .= "</body></html>";


if(@mail($sendto, $subject, $msg, $headers)) {
echo "true";
} else {
    echo "false";
}

?>

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

<form id="contact" name="contact">
<label for="email">Your E-mail</label>
<input type="email" id="email" name="email" class="txt">
<br>
<label for="msg">Enter a Message</label>
<textarea id="msg" name="msg" class="txtarea"></textarea>

<input type="button" name="submit" value="send email" onclick="sendEmail()"/>
</form>

jquery脚本:

 function sendEmail()
  {
   var emailval  = $("#email").val();
   var msgval    = $("#msg").val();

   $.ajax({
            type: 'POST',
            url: 'contactforms/send_gm.php',
            data: {email: emailval, msg: msgval}
   })
   .done(function( msg ) {
    alert (msg);
   }

php脚本:

 <?php
 $usermail = $_POST['email'];
 $content  = $_POST['msg'];
 // for rest of the part of php script view the following link
 ?>

以下是链接send html email

修改

我已action and method attr

之前删除data:{email: emailval, msg: msgval}并在javascript中email: email, msg: msg删除