简体   繁体   English

AJAX未使用$ _POST发送任何信息

[英]AJAX isn't sending any information with $_POST

I followed a tutorial on fancybox and an AJAX query for a contact form. 我遵循了有关fancybox和AJAX查询联系表单的教程。 The only thing I changed was the location of the .php file that has all of the sendmail information in. The e-mail sends when you fill out the contact form, but there's no data in it at all. 我唯一更改的是包含所有sendmail信息的.php文件的位置。当您填写联系表时,电子邮件将发送,但其中根本没有数据。

Here's the form: 形式如下:

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

And the AJAX code; 以及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>

the send_gm.php page is this; 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";
}

?>

Try something like this: 尝试这样的事情:

<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 script: 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 script: php脚本:

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

Here is the link send html email 这是发送HTML电子邮件的链接

Edit 编辑

I have remove action and method attr and in javascript now data:{email: emailval, msg: msgval} before email: email, msg: msg 我已经删除了action and method attr ,现在在javascript中, data:{email: emailval, msg: msgval}之前的email: email, msg: msg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM