after submitting a form to email i get 2 email instead 1
本问题已经有最佳答案,请猛点这里访问。
在向电子邮件提交表单后,我收到2封电子邮件,而不是1封电子邮件,我如何修复它?我只需要一封信来电邮
JS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | app.controller('threeCtrl',function($scope){ $("#subBusinessOne").click(function() { var url ="businessFormOne.php"; $.ajax({ type:"POST", url: url, data: $("form#businessFormOne").serialize(), success: function(data) { var name = $("input[name=name]").val(""); var rel= $("input[name=phone]").val(""); } }); return false; // avoid to execute the actual submit of the form. }); }); |
PHP:
1 2 3 4 5 6 7 8 9 10 | <?php $ToEmail = 'myemail.com'; $EmailSubject = 'Охрана бизнес-обьектов'; $mailheader ="From:".$_POST["email"]." "; $MESSAGE_BODY ="Имя:".$_POST["name"].""; $MESSAGE_BODY .="Телефон:".$_POST["phone"].""; mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); ?> |
我想
您应该检查表单是否已提交,然后阻止默认操作并发送Ajax请求,而不是检测按钮单击事件。您的JS代码如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | app.controller('threeCtrl',function($scope){ $("#businessFormOne").submit(function(e) { e.preventDefault(); // this is to avoid the actual submit var url ="businessFormOne.php"; $.ajax({ type:"POST", url: url, data: $("form#businessFormOne").serialize(), success: function(data) { var name = $("input[name=name]").val(""); var rel= $("input[name=phone]").val(""); } }); }); }); |
我猜你是想提出交叉Ajax请求,这也是为什么你收到2封电子邮件而不是1封的原因。
因为第一个具有方法选项的请求(用于检查是否可从其他域发送请求)和第二个具有方法日志的请求
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | app.controller('threeCtrl',function($scope){ $("#subBusinessOne").click(function(e) { e.preventDefault(); var url ="businessFormOne.php"; $.ajax({ type:"POST", url: url, data: $("form#businessFormOne").serialize(), success: function(data) { var name = $("input[name=name]").val(""); var rel= $("input[name=phone]").val(""); } }); return false; // avoid to execute the actual submit of the form. }); }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | app.controller('threeCtrl',function($scope){ $("#subBusinessOne").submit(function(e) { e.preventDefault(); var url ="businessFormOne.php"; $.ajax({ type:"POST", url: url, data: $("form#businessFormOne").serialize(), success: function(data) { var name = $("input[name=name]").val(""); var rel= $("input[name=phone]").val(""); } }); }); }); |