最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHP explode is removing the first array value - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited Dec 26, 2009 at 5:56 YOU 124k34 gold badges190 silver badges222 bronze badges asked Dec 26, 2009 at 5:53 user238757user238757
Add a ment  | 

2 Answers 2

Reset to default 6

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'];

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 ma 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:

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:

发布评论

评论列表(0)

  1. 暂无评论