vulkan vulkan - 6 months ago 43
Ajax Question

PHPMailer multiple sending mail

Im using pop3 php mailler and 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 :

var dataArray = $("form").serialize();
type: "POST",
url: "include/login.php",
dataType: "json",
data: dataArray,
success: function (data) {
if(data.result == "true"){
$('.alert strong').html("hello ";
window.location.replace("<?php if(isset($_SERVER['HTTP_REFERER'])) { echo "$_SERVER[HTTP_REFERER]";}else{echo "index.php";} ?>");
}, 1200);
}else if(data.result == "false"){
$('.alert strong').html(;
}else if(data.result == "warning"){
$('.alert strong').html(;
/// send mail button click
$('.alert strong').html("sending mail..");
$('.alert strong').html(msj);

mailing php :

$sql = "UPDATE users SET u_activation_code = '$activation_code' WHERE u_id = '$u_id'";
$query = mysqli_query($con,$sql);
$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->setFrom('fragman@aktivasyon', 'Aktivasyon');
$mail->addReplyTo('', 'activation');
$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";
echo "error query";

where is problem ?


Because of this line:


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. You should bind the click event ONCE.

You could also do:


And then bind the click event again, to prevent it from happening.


You're binding a click event for every BUTTON element existing in the DOM:


I also suggest that you specify this event to a button with unique ID, and not for every button. 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)

What you should do is to add an ID attribute to the button that when you click on trigger the login 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){