2

I have form where i am sending multiple checkbox items as array to controller,

<?php
    foreach($groupsArray as $group)
        {
?>
<label>
<input type="checkbox" class="icheck" name="groups[]" id="groups" value="<?php echo $group["id"];?>"> <?php echo $group['name']?> </label>

<?php
}
?>

Everything works fine for checkbox values update in database,

Now, what i am doing is, getting values from database and i need to check values which are stored in database,

I am getting below json response from PHP<

groups: [{user_id: "2", group_id: "4", id: "4", name: "system Creators",…}]

Below i used as AJAX

if(objData.groups[0].group_id == $("#groups").val())
    {
        $("#groups").iCheck('check');
    }

With this $("#groups").val(), it always takes values of first checkbox, so there is problem, How can i compare values for all checkboxes with Json? Also if groups array will have multiple values, means multidimensional array, more groups then?

Thanks in advance!

4 Answers 4

1

You need to be able to know which checkbox is related to which database value.

That is what the ID is used for on the checkbox. You have them all the same being "groups" - which is bad practice.

Use: (note the dynamic id attribute)

<?php
    foreach($groupsArray as $group)
        {
?>
<label>
<input type="checkbox" class="icheck" name="groups[]" id="chk<?php echo $group["id"];?>" value="<?php echo $group["id"];?>"> <?php echo $group['name']?> </label>

<?php
}
?>

Then loop around your group object from the database:

for(var i = 0; i < groups.length; i++) {
   var chk = document.getElementById("chk" + groups[i].id);
   chk.checked = true;
}
Sign up to request clarification or add additional context in comments.

7 Comments

what will be value of groups.length? Is it array length?, If Yes, then how it will check 5 checkboxes when i have array of only 2?
Yes it's your group array and length . If you only have 2 items in the database, why would you "check" 5? If you mean like check/tick - you'd only want to tick the ones in the db yeah? If there a 5 checkboxes it will only check the ones you have saved.
Hmmm, seems working now. It was typical iCheck plugin which lead me to change last line of your for loop, good. BTW, is dynamic id attribute is unique? I have same type of two pair of checkbox values, so i need to apply for another pair as well, same function.
So you're saying the id $group[id] is not unique? So you can have 2 ids the same? If that's the case you need some sort of unique identifier. Perhaps the combination of group_id and id? Not totally sure what you mean.
i mean i have another set of checkboxes permissions, like groups. And i am also getting another JSON permissions, so i need to duplicate another forloop with permissions.length, so i want to know what this dynamic id attribute does? Can is it be reused in another for loop?
|
1

Try this:

PHP:

<?php
$count=0;
foreach($groupsArray as $group)
{
?>
    <label>
    <input type="checkbox" class="icheck" name="groups[]" id="groups<?php echo $count;?>" value="<?php echo $group["id"];?>"> <?php echo $group['name']; ?> </label>
<?php 
    $count++;
}
?>

Jquery:

for(var i=0;i<objData.gourps.length;i++){
    if(objData.groups[i].group_id == $("#groups"+i).val())
    {
        $("#groups"+i).iCheck('check');
    }
}

7 Comments

Thanks, This seems very easy and quick near to my answer,
Um aren't only the checked values stored in the database? This won't work.
Agreed with @Shaun, i mixed examples of shaun and yours, but still have problem, first of all starting with i=0, there is not groups0 because there is no group_id = 0 in database, 2nd- if i have only 2 values in array and 5 checkboxes, then it will not loop for 5 times, so rest 3 checkboxes will be skipped!, still i have same issue!
So Do you want to store all the values in database either checked or not??
So don't mix - just use mine ;) I don't use the index of the loop, nor touch your group ids. Element Ids should always be unique
|
0

First, change the groups checkbox's id to it's class, so use class="groups" instead of id="groups", like this:

<label><input type="checkbox" class="icheck" name="groups[]" class="groups" value="<?php echo $group["id"];?>"> <?php echo $group['name']?> </label>

Next, use the jQuery .each method to check all the checkboxes that is found in the JSON object like this:

$.each(objData, function(n, i){
    if(i.group_id == $(".groups[name="+n+"]").val()){
        $(".groups[name="+n+"]").iCheck('check');
    }
});

Comments

0

Finally, this worked! Find only those checkboxes which are coming from DB and check those.

for(var i = 0; i < objData.groups.length; i++) 
{
    $("#groups" + objData.groups[i].group_id).iCheck('check');
}

Thanks all for support!

Comments

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.