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.

Beeelze
  • 503
  • 1
  • 9
  • 25
  • 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()`. – Don McCurdy Sep 24 '14 at 15:05
  • 1
    "...duplicate e-mail addresses from the array" What specific variable in the above code are you reffering to? – Steve Sep 24 '14 at 15:05
  • This is a lot of code, but it looks like your problem could be expressed [in just a few words](http://stackoverflow.com/questions/16063590). Also, you should show what you have tried, because I fail to guess what could go wrong with something [as simple as `array_unique()`](http://stackoverflow.com/q/307650/1446005). – RandomSeed Sep 24 '14 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. – Beeelze Sep 24 '14 at 21:33
  • @MarijnvanGool once again, what variable has duplicates in it – Steve Sep 24 '14 at 21:35
  • @Steve the array $testarray[] – Beeelze Sep 25 '14 at 07:18
  • I think the problem lies at the point where I send the checkbox values to the php page with the ajax call. Therefore I think all the efforts of removing duplicates do not work. – Beeelze Sep 25 '14 at 08:12
  • @DonMcCurdy I edited the php code where i used array_unique. – Beeelze Sep 25 '14 at 08:19
  • Correct, your problem is your javascript. You loop through all you check boxes and send a separate ajax request for each one that is checked. If the user should only be able to select one or the other, use radio buttons instead – Steve Sep 25 '14 at 09:13
  • @Steve That's right. But the user should be able to check more than one. How would I solve my problem in the javascript code? – Beeelze Sep 25 '14 at 09:22
  • Well your php only allows for one of two options, urgent or non urgent, so I'm not sure why a user would check both, that seems illogical – Steve Sep 25 '14 at 13:05
  • I fixed my problem with rewriting the jQuery script and the php page listed above. "var str = $(".fooby:checked").serializeArray().valueOf();" is what i send to the php page now. In the SQL query i used DISTINCT to get unique values from the database. – Beeelze Sep 26 '14 at 09:54

2 Answers2

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?

laminatefish
  • 5,197
  • 5
  • 38
  • 70
0

could something like this...

$noDupes = array_map("unserialize", array_unique(array_map("serialize", $testarray)));
Marty
  • 4,619
  • 2
  • 24
  • 35