У меня есть таблица как
<tr>
<td><?php echo ++$count ?> </td>
<td style="text-align:center;"><input type="checkbox" name="id[]" id="id[]" class="checkboxes" value="<?php echo $row['id']; ?>" ></td>
<td><?php echo $row['fname'] ?></td>
<td><?php echo $row['mobile'] ?> </td>
<td><?php echo $row['email'] ?> </td>
<td><?php echo $row['regdate'] ?> </td>
<td><button value="<?php echo $row['id'] ?>" id="userdelete" class='userdelete btn btn-danger'>Delete</button></td>
</tr>
, где я получаю идентификатор пользователя, полное имя, номер мобильного телефона, адрес электронной почты из базы данных.
Я хочу выбрать нескольких пользователей, используя флажок, и отправить сообщение с помощью phpmailer , Я использую Ajax для этого.
$(document).on('click', '#sendemail', function(ev){
ev.preventDefault();
var SlectedList = new Array();
$("input[name='id[]']:checked").each(function() {
SlectedList.push($(this).val());
});
$emailbody=$('#emailbody').val();
$emailheader=$('#emailheader').val();
$.ajax({
type: "POST",
url: "subscribehere.php",
data: {
emailbody: $emailbody,
emailheader: $emailheader,
id: SlectedList,
sendemail: 1,
},
success: function(response){
$('.subscriber_msg').html(response);
$('#emailModal').modal('hide');
}
});
});
и мой код php
<?php
$errmsg = " ";
if (isset($_POST['sendemail'])) {
$header=$_POST['emailheader'];
$ebody=$_POST['emailbody'];
if ( count($_POST['id']) > 0 ) {
foreach($_POST['id'] as $val){
$sql = "SELECT * FROM subscribers WHERE id=$val ";
$query=mysqli_query($conn,$sql) or die(mysqli_error($conn));
if ( $row1= mysqli_fetch_assoc($query) ) {
$email=$row1['email'];
$mobilenumber=$row1['mobile'];
require 'class/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); //Sets Mailer to send message using SMTP
$mail->Host = 'smtpout.secureserver.net'; //Sets the SMTP hosts of your Email hosting, this for Godaddy
$mail->Port = '80'; //Sets the default SMTP server port
$mail->SMTPAuth = true; //Sets SMTP authentication. Utilizes the Username and Password variables
$mail->Username = '***********'; //Sets SMTP username
$mail->Password = 'vvnagar123'; //Sets SMTP password
$mail->SMTPSecure = ''; //Sets connection prefix. Options are "", "ssl" or "tls"
$mail->From = '********'; //Sets the From email address for the message
$mail->FromName = 'Radio SAREGAMA'; //Sets the From name of the message
$mail->AddAddress($email, $mobilenumber); //Adds a "To" address
$mail->WordWrap = 50; //Sets word wrapping on the body of the message to a given number of characters
$mail->IsHTML(true); //Sets message type to HTML
$mail->Subject = $header; //Sets the Subject of the message
$mail->Body = $ebody;
//An HTML or plain text message body
$mail->Send();
exit;
} else {
echo "Error while sending.". mysqli_error();
}
$conn->close();
}
echo "totally ".count($_POST['id']). " Emails has been sent successfully";
} else {
echo "You need to select atleast one checkbox to delete!";
}
}
?>
, когда я выбираю одного пользователя, электронная почта отправляется успешно, но при выборе нескольких пользователей отправляется только одна письмо первому пользователю и игнорировать оставшееся. где я делаю неправильный шаг, пожалуйста, помогите ...