# Tick checkbox if database field contains value

**URL:** <https://forum.kirupa.com/t/tick-checkbox-if-database-field-contains-value/299674>\
**Category:** programming\
**Created:** [November 9, 2009, 2:59pm UTC](https://forum.kirupa.com/t/tick-checkbox-if-database-field-contains-value/299674 "2009-11-09T14:59:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gilo87](https://avatars.discourse-cdn.com/v4/letter/g/ed8c4c/32.png) [@gilo87](https://forum.kirupa.com/u/gilo87)\
**Post date:** [November 9, 2009, 2:59pm UTC](https://forum.kirupa.com/t/tick-checkbox-if-database-field-contains-value/299674/1 "2009-11-09T14:59:21Z")

</div>

Hi Guys,

I have used a bit of PHP but I’m not very good at it!

I was hoping someone could help with my problem.

I have a table with several checkboxes.  
The user can select the boxes, the values are made into a comma separated string and added to a DB field.  
I want the checkboxes that have been previously checked (the field contents) to be checked when the user revisits that page.

So it’s something like.

if($field CONTAINS $checkBoxValue){  
checkbox = checked  
}

I am having trouble thinking of a way of implementing this.

Here is my form code:

```auto
<form method=post action=''>
        
    <table width="100">
    <tr>
    <td width="60">Identity</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Identity'></td>
    </tr>
    
    <tr>
    <td width="60">Stationery</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Stationery'></td>
    </tr>
    
    <tr>
    <td width="60">Print</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Print'></td>
    </tr>
    
    <tr>
    <td width="60">Packaging</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Packaging'></td>
    </tr>
    
    <tr>
    <td width="60">Exhibition</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Exhibition'></td>
    </tr>
    
    <tr>
    <td width="60">Web</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Web'></td>
    </tr>
    
    <tr>
    <td width="60">3D</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='3D'></td>
    </tr>
    
    </table><br>
    
    <input type="submit" name="editFields" value="Select">
    
    
    </form>

```

Here is my PHP code:

```php
$checked = "";

$WordsToFind = "Identity,Stationery,Print,Packaging,Exhibition,Web,3D";
$WordsToFind = explode(',',$WordsToFind);

$text = "Identity,Stationery,Print,Packaging,Exhibition,Web,3D";

for($i = 0; $i<count($WordsToFind); $i++) {
    if(stripos($text,$WordsToFind[$i])) { 
        $checked='checked'; break; 
    }
}

```

NOTE: $WordsToFind is actually pulled from a DB. I have written it out so you can see the values.

The PHP works, just not in the way I would like. i.e. $checked does equal “checked” but I need it to only equal checked for the correct checkboxes.

Thanks in advance.

Sean
