0

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.

12
  • Could you include the code where you tried using array_unique()? Don't forget that it returns a new array, instead of modifying the one you pass in, unlike sort(). Commented Sep 24, 2014 at 15:05
  • 1
    "...duplicate e-mail addresses from the array" What specific variable in the above code are you reffering to? Commented Sep 24, 2014 at 15:05
  • This is a lot of code, but it looks like your problem could be expressed in just a few words. Also, you should show what you have tried, because I fail to guess what could go wrong with something as simple as array_unique(). Commented Sep 24, 2014 at 15:13
  • @RandomSeed It should be expressed in just a few words. What I think goes wrong here is that when i check 2 checkboxes, i get 2 arrays that are put into one. I might have a multi dimensional array here, but i'm not sure. Commented Sep 24, 2014 at 21:33
  • @MarijnvanGool once again, what variable has duplicates in it Commented Sep 24, 2014 at 21:35

2 Answers 2

0

I don't know why array_unique isn't working for you, but you could try it the old fashioned way and not add them to the array in the first place, with something like:

if(!in_array($theEmailAddress, $testarray)){
    // add to array
}
else{
    // more than likely a duplicate
}

Out of curiosity, what does array_unique do when you've used it?

Sign up to request clarification or add additional context in comments.

Comments

0

could something like this...

$noDupes = array_map("unserialize", array_unique(array_map("serialize", $testarray)));

1 Comment

tried this, but the server still sends 2 e-mails to the same recipient

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.