Jump to content

Need Help Reloading Previously Selected Checkboxes


Go to solution Solved by jostclan,

Recommended Posts

Hello fellow PHP developers.  I have been stuck on a problem I just can not figure out.  I am developing a couple forms (insert and update) that users fill out.  On this form is general user information (first and last name fields, etc.) as well as a checkbox group based on an available “groups” table.  When the user fills out the form and makes their checkbox selections I write the user information to a user table, and write the user’s checkbox choices to a user_group_xref  table (unique_id, fk_user_id, fk_group_id) for as many choices as the user selected.  The fk_user_id is a foreign key to the user table, and the fk_group_id is a foreign key to the group table.

User_table

Unique_id, first_name, last_name, address

Groups_table

Unique_id, group_description, active_flag

User_group_xref table

Unique_id, fk_user_id, fk_group_id

 

This all works correctly for the insert, and I can make the selections out of the tables no problem.  To get the users previous selections, I query the xref table and join it back to the group table for the description. My user_groups query returns g.unique_id, g.group_description for the appropriate user. I generate the checkboxes with the following code:

<?php

do { ; ?>

                <br />

                <label>

                                <input type="checkbox" name="admin_groups[]" value="<?php echo $row_groups['pkID']; ?>" id="admin_groups_<?php echo $row_groups['pkID']; ?>"

                                tabindex="<?php echo $iTabIndexCounter;?>" />

                                <?php echo $row_groups['group_description']; ?>

                </label>

<?php } while ($row_groups = mysql_fetch_assoc($groups)); ?>

 

This works fine in generating the checkboxes.  How do I at the same time check those checkboxes previously selected?  The only thing I can think of is to show only those checkboxes actually selected, but I want all checkboxes displayed, and those previously selected checked.  Any suggestions would be greatly appreciated. 

 

 

 

 

Hey,

 

First off, welcome to the forum!

 

The first thing I am going to do is post your code below in my post so that everyone can read it better. On your next post you can do that by using the <> toggle in the text editor.....

<?php
do { ; ?>
                <br />
                <label>
                                <input type="checkbox" name="admin_groups[]" value="<?php echo $row_groups['pkID']; ?>" id="admin_groups_<?php echo $row_groups['pkID']; ?>"
                                tabindex="<?php echo $iTabIndexCounter;?>" />
                                <?php echo $row_groups['group_description']; ?>
                </label>
<?php } while ($row_groups = mysql_fetch_assoc($groups)); ?>

There easier to read.

 

As for your problem, you are going to want to write an if statement for the data you wish to receive from your query. Seeing as you are using the checkbox input and not radio buttons you are going to have to put the word "checked" somewhere inside of your <input>. See this link.

 

So what I would do is assign your "value" from the <input> to a variable.

$x=$row_groups['pkID'] 

Then after it write and if statement to see if it exists. Then if it does has a new variable called $checked="checked"; and echo that inside of the input. If $checked exists it will show up in your input.

 

If you need help with that let me know

Edited by computermax2328

Use LEFT JOIN with xref table

$sql = "SELECT g.group_description, g.unique_id, x.fk_group_id
    FROM groups g
    LEFT JOIN user_group_xref x
        ON g.unique_id = x.fk_group_id
        AND x.fk_user_id = $currentUser
    WHERE g.active_flag = 1"

$res = $mysqli->query($sql);
while (list($desc, $gid, $xid) = $res->fetch_row()) {
    $chk = $xid ? "checked='checked'" : '';  // checked if present in xref table
    echo "<input type='checkbox' name='group[]' value='$gid' $chk> $desc<br>";
}
  • Solution

Thanks folks! I appreciate the welcome and I am impressed on the quick responses.  Between the responses, a small light bulb went on.  Each user can have one or more groups to which they belong.  Again, I wanted to show all available groups as checkboxes, and have each checked (one or more) based on the user’s prior choices.

 

I solved this like this:

<?php
do { 

$iTabIndexCounter+=5; 
$isChecked=false;
mysql_data_seek($user_groups,0);
while($row_user_groups = 
mysql_fetch_assoc($user_groups)){
                
if($row_user_groups['pkID'] == $row_groups['pkID']) {
                                
$isChecked = true;
                
}
}
?>
                
<br />
                
<label>
                                
<input <?php if($isChecked == true) {echo "checked=\"checked\"";} ?> 

                                
type="checkbox" name="admin_groups[]" value="<?php echo $row_groups['pkID']; 
?>" id="admin_groups_<?php echo $row_groups['pkID']; ?>" 

                                
tabindex="<?php echo $iTabIndexCounter;?>" />
                                
<?php echo $row_groups['group_description']; ?>
                
</label>
<?php 
} while ($row_groups = mysql_fetch_assoc($groups)); ?>

I tested this on four users and all are returning the proper values. It may not be the most elegant or tight code, but it is where I am currant at in my skill set. I have not seen the while(list command so this one is new to me and I will look at this.  Again I appreciate you being there and giving me ideas.

Edited by jostclan
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.