I have created a form with 3 checkboxes. Each checkbox correspondents with a mailing list in the database. It is possible that I get duplicate values when I check 2 checkboxes. I have tried both the PHP function array_unique() and the jQuery.unique() functions to remove all duplicate e-mail addresses from the array.
jQuery:
<script>
$("#sendMessage").submit(function(e) {
$("input[type=checkbox]").each(function() {
if($(this).is(":checked")) {
var string = $(this).val();
$.ajax({
url: "include/checkbox.inc.php",
type: "POST",
data: ({query: string, type: "nonurgent"}),
dataType:"JSON",
success: function(data) {
keep_cb = data;
mail(keep_cb);
}
});
}
});
});
include/checkbox.inc.php:
<?php
// this page checks which mailing group is selected, urgent or non urgent
include("mysession_start.inc.php");
include("db.inc.php");
$testarray = array();
$noDupes = array();
if(isset($_POST['query'])) {
$checkbox_value = $_POST['query'];
}
if(isset($_POST['type'])) {
$type = $_POST['type'];
}
if($type == "nonurgent") {
$cb_query = "SELECT email_nonurgent FROM client_addresses WHERE $checkbox_value=1";
if($resultq = mysqli_query($link, $cb_query)) {
$s_count = mysqli_num_rows($resultq);
while($rowq = $resultq->fetch_array()) {
$testarray[] = $rowq["email_nonurgent"];
$noDupes = array_unique($testarray);
}
print json_encode($noDupes);
}
} else {
$cb_query = "SELECT email_urgent FROM client_addresses WHERE $checkbox_value=1";
if($resultq = mysqli_query($link, $cb_query)) {
$s_count = mysqli_num_rows($resultq);
while($rowq = $resultq->fetch_array()) {
$testarray[] = $rowq["email_urgent"];
}
print json_encode($testarray);
}
}
?>
With 2 checkboxes clicked, it's possible that I get duplicate e-mail addresses that are in the same array ($testarray in the php page). I have searched all over the web, but couldn't find out what I am doing wrong.