views:

336

answers:

2

Ok, first off, I applogize if this question has been asked before, I've spent the last 12 hours sifting through hundreds of forum threads and haven't found one that answers this question exactly yet so here goes.

I have a page that I built that has a userlist that is autopopulated as users log in, and each users has a checkbox that allows the host to select users and removed them if needed. I am using jQuery to send an array string to a php page where I am attempting to explode the string, walk through the userids and remove the passed ids from our database. It all works exceedingly well, unless I select more than one user. When I select more than one user and pass the array to the php handler, it explodes the array but only keeps the last value in the array, the rest of it is lost somewhere in the blackhole of scripting.

Here is my code from my javascript page:

function rem_user() {  
var ids = $('input:checkbox[id=del]:checked').map(function(){
    return this.value
}).get();
alert(ids);
jQuery("#rem_msg").load(controlpage, {AdmFnc: "rem_user", del_ids: ids}, getUsers);
}

this array is then passed to the php controlling page where it is handled by:

if (strcmp($AdmFnc, rem_user) == 0){
echo "";
$ids = trim($_POST['del_ids']);
$ids = explode(',',$ids);
if(is_array($ids)){
   foreach($ids as $userid){
      mysqli_query($dbc, "UPDATE webcastlogin SET logoutTime = '$current_time' WHERE userid = '$userid'") or die('Error setting logout time '.mysqli_error($dbc));
      mysqli_query($dbc, "DELETE FROM weekly_users WHERE userid = '$userid'") or die('Error removing users '.mysqli_error($dbc));
    }
    echo 'Selected IDs removed';
} else if (empty($ids)) {
    echo 'Code is wrong, no array sent';
} else {
    mysqli_query($dbc, "UPDATE webcastlogin SET logoutTime = '$current_time' WHERE userid = '$ids'");
    mysqli_query($dbc, "DELETE FROM weekly_users WHERE userid = '$ids'");
    echo 'Selected ID removed';
}
}

As you can see on my javascript, i have an alert set up so i can verify that the information being sent is correct, as far as i can tell from that alert, it's a "," delimited array so that's what i'm using on the php side to explode it.

I am at my wits end trying to figure out why when I run this thru the foreach loop i only get the value of the final item in the array.

Any help would be greatly appreciated and may save my hair for some future coding challanges.

Thanks

+3  A: 

You should convert your array to string before making the Ajax request, you can do it with the join method:

function rem_user() {  
  var ids = $('input:checkbox[id=del]:checked').map(function(){
    return this.value
  }).get().join(); // ids now is a comma separated string, made from the array

  alert(ids);
  jQuery("#rem_msg").load(controlpage, {AdmFnc: "rem_user",
    del_ids: ids},
    getUsers);
}

What it's happening is that when you send the Array object, the value of del_ids gets repeated on the Ajax request POST parameters:

img1

That's why you get only the last element, since it's really the same parameter.

But if you make a string from your array, it will post the value of the parameter as you expect it on the server-side:

img2

CMS
Great answer. I also had no idea the default of the `join` method was to comma separate. Great to know!
Doug Neiner
Thanks Doug, you can use it without worries, since it is guaranteed by the spec! http://bclary.com/2004/11/07/#a-15.4.4.5
CMS
Thanks for the reply, I have tested your answer, and while the array is being sent to php in a string, and I have tested the response, the foreach loop is still only affecting the final value in the string. I'm wondering if there is something wrong with the foreach loop.
Chris
+5  A: 

Just change your key name for del_ids in the jQuery load call to include an empty set of brackets:

jQuery("#rem_msg").load(controlpage, {AdmFnc: "rem_user", "del_ids[ ]": ids}, getUsers);

Now, in the PHP, instead of using trim and explode, the values will already be in an array, so you can access them directly:

$ids = $_POST['del_ids'];
Doug Neiner
Thanks for the reply, I have tested your answer, however when I go to the php script, i can't seem to access the full array that's being sent, when i run the variable thru the "if" statement, it's only being recognized as a single value.
Chris
Ok, i take that back, I just didn't set it up correctly, there needs to be a space in the brackets for php to recognize it as an array, once i did that it worked like a charm. Thanks so much!
Chris
Hey you are welcome. I want to update my answer correctly, so you are saying it needs to be `"del_ids[ ]"` ?
Doug Neiner
that's correct, php is a little picky about recognizing array variables. it reads `"del_ids[]"` as single variable, it needs the space in the brackets to recognize it as an array variable.
Chris
+1 for the clean solution.
Chandra Patni