drake900918 2016-05-07 11:02
浏览 42

Ajax通过PHP提交表单并验证/发送电子邮件

I am working on a html contact form and using the javascript code below to validate the form when the submit button is clicked:

function leftValue(e, t, n) {
$(this).text(t.parent()[0].style.left)
}
$(document).ready(function() {
required = ["name", "email"];
email = $("#email");
errornotice = $("#error");
emptyerror = "Required Field";
emailerror = "Required Field";
$("#contact-us").submit(function() {
for (i = 0; i < required.length; i++) {
    var e = $("#" + required[i]);
    if (e.val() == "" || e.val() == emptyerror) {
        e.addClass("form-error");
        e.val(emptyerror);
        errornotice.fadeIn(750)
    } else {
        e.removeClass("form-error")
    }
}
if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-  Z0-9]{2,4})+$/.test(email.val())) {
    email.addClass("form-error");
    email.val(emailerror)
}
if ($(":input").hasClass("form-error")) {
    return false
} else {
    errornotice.hide();
    return true
}
});
$("input").focus(function() {
if ($(this).hasClass("form-error")) {
    $(this).val("");
    $(this).removeClass("form-error")
}
})
});

HTML

<form action="" method="post" name="contact-us" id="contact-us"> 
<input type="text" id="name" name="name"/>
<input type="text" id="email" name="email"/>
</form>
<div id="success">Thank you for your message!</div>

Once the validation is passed and the submit button is clicked again, the data is posted via PHP and emailed using this code:

<?php  
if(isset($_POST['submit']))
{
$to="test@test.com";
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$body="Name: $name 

 Email Address: $email 

";
$sent=mail($to, $body); 
}
?>

I am trying to now using ajax to carry out these functions and display a confirmation message on the same page without reloading. I have tried the code below which allows the form to be validated but when the validation passes and submit is clicked again, the page just reloads and doesn't display the confirmation message contained in the #success div?

$(document).ready(function() {
$("#contact-us").submit({
submitHandler: function() {
$.post("<?php echo $_SERVER[PHP_SELF]; ?>", //post
        $("#contact-us").serialize(), 
        function(data){
          if (data == 'Sent') {
            $("#success").fadeIn(); 

        }
        //
    });
    return false;
}
});
});
  • 写回答

1条回答 默认 最新

  • dongzha0149 2016-05-07 11:08
    关注

    Did you try this: event.preventDefault();

    $(document).ready(function() {
    $("#contact-us").submit(function(event) {
    
    event.preventDefault();
    
    $.post("<?php echo $_SERVER[PHP_SELF]; ?>", //post
            $("#contact-us").serialize(), 
            function(data){
              if (data == 'Sent') {
                $("#success").fadeIn(); 
    
            }
        });
        return false;
    });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作