简体   繁体   English

PHPMailer多个发送邮件

[英]PHPMailer multiple sending mail

Im using pop3 php mailler and ajax. 我使用pop3 php mailler和ajax。 There is no problem for sending mail but when I press sent mail button it incease number of mails. 发送邮件没有问题,但是当我按下发送邮件按钮时,它会增加邮件数量。 So whats the problem 所以有什么问题

my ajax code : 我的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);
                 },
                });
              });
             }
            },
          });
      });

mailing php : 邮寄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";
    }

where is problem ? 哪里有问题?

Because of this line: 因为这条线:

$('body').on('click','#activation',function(e){

On every click on a button, you're binding another click event to #activation making it run multiple times on every click on that button. 每次单击按钮时,您都会将另一个click事件绑定到#activation使其在每次单击该按钮时多次运行。 You should bind the click event ONCE. 您应该绑定click事件ONCE。

You could also do: 你也可以这样做:

$('body').off('click','#activation');

And then bind the click event again, to prevent it from happening. 然后再次绑定click事件,以防止它发生。

EDIT 编辑

You're binding a click event for every BUTTON element existing in the DOM: 您正在为DOM中存在的每个BUTTON元素绑定一个click事件:

$("button").click(function(e){

I also suggest that you specify this event to a button with unique ID, and not for every button. 我还建议您将此事件指定为具有唯一ID的按钮,而不是每个按钮。 Because when you click on the #activation button, it will ALSO trigger the call to the login ajax (Since it's also a button element) 因为当你点击#activation按钮,它将触发调用登录AJAX(因为它也是一个按钮元素)

What you should do is to add an ID attribute to the button that when you click on trigger the login ajax: 你应该做的是向按钮添加一个ID属性,当你点击时触发登录ajax:

<button id="login-btn">Log In</button>

And then, change the above binding to: 然后,将上面的绑定更改为:

$("#login-btn").click(function(e){ // INSTEAD OF THE CURRENT $("button").click(function(e){

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

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