我使用pop3 php mailler和ajax。发送邮件没有问题,但是当我按下发送邮件按钮时,它会增加邮件数量。那么问题是什么
我的ajax代码:
$("button").click(function(e){
$('.alert').slideUp(100);
e.preventDefault();
var dataArray = $("form").serialize();
$.ajax({
type: "POST",
url: "include/login.php",
dataType: "json",
data: dataArray,
success: function (data) {
if(data.result == "true"){
$('.alert').removeClass('alert-danger').addClass('alert-success').slideDown(200);
$('.alert strong').html("hello "+data.name);
setTimeout(function(){
window.location.replace("<?php if(isset($_SERVER['HTTP_REFERER'])) { echo "$_SERVER[HTTP_REFERER]";}else{echo "index.php";} ?>");
}, 1200);
}else if(data.result == "false"){
$('.alert').removeClass('alert-success').addClass('alert-danger').slideDown(200);
$('.alert strong').html(data.name);
}else if(data.result == "warning"){
$('.alert').removeClass('alert-warning').addClass('alert-danger').slideDown(200);
$('.alert strong').html(data.name);
/// send mail button click
$('body').on('click','#activation',function(e){
e.preventDefault();
$.ajax({
beforeSend:function(){
$('.alert strong').html("sending mail..");
},
url:"include/activation_mail.php",
type:"post",
data:{u_id:data.userid,u_pass:data.u_pass},
success:function(msj){
$('.alert').removeClass('alert-danger').addClass('alert-success').slideDown(200);
setTimeout(function(){
$('.alert strong').html(msj);
},1000);
},
});
});
}
},
});
});
邮寄php:
$sql = "UPDATE users SET u_activation_code = '$activation_code' WHERE u_id = '$u_id'";
$query = mysqli_query($con,$sql);
if($query){
$sql="SELECT * FROM users WHERE u_id='$u_id'";
$query = mysqli_query($con,$sql);
$row = mysqli_fetch_row($query);
$id = $row[0];
$name = $row[1];
$surname = $row[2];
$fullname = "$row[1] $row[2]";
$username = $row[5];
$email = $row[6];
$icerik = "aktivasyon maili <a href=''>activation</a>";
$mail = new PHPMailer();
$mail->CharSet='utf-8';
$mail->setFrom('fragman@aktivasyon', 'Aktivasyon');
$mail->addReplyTo('mymail@gmail.com', 'activation');
$mail->addAddress($email,$fullname);
$mail->Subject = 'activation link';
$mail->AltBody = 'This is a plain-text message body';
$mail->Body = "click active <a href=''>clicked</a> ";
//send the message, check for errors
if (!$mail->send()) {
echo "error sending: " . $mail->ErrorInfo;
} else {
echo "send mail";
}
}else{
echo "error query";
}
哪里有问题?
答案 0 :(得分:3)
由于这一行:
$('body').on('click','#activation',function(e){
每按一次按钮,您就会将另一个click
事件绑定到#activation
,使其在每次点击该按钮时多次运行。你应该绑定点击事件ONCE。
你也可以这样做:
$('body').off('click','#activation');
然后再次绑定click事件,以防止它发生。
修改强>
您正在为DOM中存在的每个BUTTON元素绑定一个click事件:
$("button").click(function(e){
我还建议您将此事件指定为具有唯一ID的按钮,而不是每个按钮。因为当你点击#activation
按钮时, ALSO 会触发对登录ajax的调用(因为它也是一个按钮元素)
你应该做的是为按钮添加一个ID属性,当你点击时触发登录ajax:
<button id="login-btn">Log In</button>
然后,将上述绑定更改为:
$("#login-btn").click(function(e){ // INSTEAD OF THE CURRENT $("button").click(function(e){