Hi People,

I've got a small query regarding checkboxes.

I have a form, where there are input box, textarea, radio buttons and checkboxes.

After form submit, Insert query gets fired and data gets inserted properly. I'm storing checkbox value as a string, I mean, the checkbox array... I'm converting it as string using the "implode" function and then storing it in the database table.

Now the problem is, I wan't to edit those details, so I've created the same form... and I'm fetching those stored records and populating them inside the textboxes. I've got no issues with textbox and textarea. Issue is checkbox data. I can fetch that stored string, use explode function...and later on display as checkboxes... but how to display those checkbox options which the user never checked when he had initially filled the form...?

Its like, say in my form...

Name: Albert //--------------- textbox

Age: 27 // --------------- textbox

Contact: 123456 // --------------- textbox

Hobbies: // ---------------checkboxes
Swimming
Trekking
Scuba Diving
Birding

...............

Now say i selected only Swimming and Trekking... those values will be posted.. and in the database the string will be like..... Swimming,Trekking

Now to display this back as checkboxes in the editable form...using explode I need to make the string back to checkboxes...(I want to show this as checked by default)... How to display the other options which i never checked .... in this case Scuba Diving and
Birding... I mean I wanna display them in the form (and also as unchecked)....

Thanks.

First off, I wouldn't store comma-delimited lists in a database field.
That said, here is some code for you to try.

<?php 

$hobbies = array(
    'Swimming',
    'Trekking',
    'Scuba Diving',
    'Birding'
);

//simulate your database field result
$dbResultHobbies = 'Swimming,Trekking,Birding';

$dbResultHobbiesArray = explode(',',$dbResultHobbies);

?>

<form>
    <?php foreach($hobbies as $hobby): ?>
        <?php if(in_array($hobby,$dbResultHobbiesArray)): ?>
            <p><input name="hobby[]" type="checkbox" value="<?php echo $hobby; ?>" checked="checked"> <?php echo $hobby; ?></p>
        <?php else: ?>
            <p><input name="hobby[]" type="checkbox" value="<?php echo $hobby; ?>"> <?php echo $hobby; ?></p>
        <?php endif; ?>
    <?php endforeach; ?>
</form>
commented: Thanx Sir..... +2

Hi Albert what I have done recently not sure what you are saving in the db
The checkbox name was active and I checked that the value was set and the checkbox was on as this is the default value when checked.
I wrote the values 1 for active 0 for non active to the db.

The second part to this is the data is read from the db and passed back to your edit form and the values are checked for the set value and ie 1 the button are set to : echo "checked='checked.

Hope this helps

d

 if (isset($_REQUEST["active"]) && $_REQUEST['active']=='on')
        {
            $partner->set_active(1);
        }
        else
        {
            $partner->set_active(0);
        }






<div>
                <label for="active"><span>Show active:</span> </label>
                <input type="checkbox" class="active"  name = "active" id="active"
                    <?php
                    if($active == 1)
                    {
                       echo "checked='checked'";
                    }?>
                    >
            </div>

`

Member Avatar for diafol

You could give your checkboxes values and add them and test with bitwise operators:

<input type="checkbox" name="hobby[]" id="swimming" value="1" /> <label for "swimming">Swimming</label>
<input type="checkbox" name="hobby[]" id="trekking" value="2" /> <label for "trekking">Trekking</label>
<input type="checkbox" name="hobby[]" id="scuba_diving" value="4" /> <label for "scuba_diving">Scuba Diving</label>
<input type="checkbox" name="hobby[]" id="birding" value="8" /> <label for "birding">Birding</label>

Then your formhandling routine:

$hobbies = (isset($_POST['hobby'])) ? array_sum($_POST['hobby']) : 0;

That's then stored in a single DB field as 14 (for trekking, scuba_diving and birding) or 3 (for swimming and trekking).
You can retrieve data in the following way:

$totalHobbies = 14; //from DB
//$hobbyItems from DB table hobbies - listing all current hobbies
//with structure like: 1 => array(1, swimming, Swimming), 2=>array(2, trekking, Trekking), 3=>(4,scuba_diving, Scuba Diving)

$checks = '';
foreach($hobbyItems as $v)
{
    $chk = ($v[0] & $totalHobbies) ? ' checked="checked"' : '';
    $checks .= "<input type='checkbox' name='hobbies[]' id='{$v[1]}' value='{$v[0]}'$chk /> <label for='{$v[1]}'>{$v[2]}</label>"
}

echo $checks;

SQLs use bitwise operators too, so if you need to retrieve a records that contain scuba diving:

SELECT ... WHERE = $scuba & `hobbies`

Anyway, just off the top of my head. Could be useful - can avoid the one checkbox, one table field scenario.

@ paulkd: I must say that was bang on target....... Perfect solution... Thanx Sir.... Thanx a lot...

@ davidjennings and diafol : Thanx for your time and consideration guys...

All you guys made it look very simple.... Thanx...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.