Checked dynamic check box

sorry i’m new in php
i have created dynamic checkbox

$rsc = mysql_query($sqlc);

while($rowc = mysql_fetch_array($rsc))
echo "<input type=\\"checkbox\\" name=\\"dataid\\" value=\\"".$rowc["data_no"]."\\" />"; echo $rowc['data_name'];
		echo "<br />";

Whether it have value or not be not checked how solved this?

Unchecked checkbox:

<input type="checkbox" name="dataId" value="1" />

Checked checkbox:

<input type="checkbox" name="dataId" value="1" checked="checked" />

That should give you a hint where you went wrong :slight_smile:

what ???

Is that all you have to add? :rolleyes:

What furicane gave you is the completed, full code for a checked checkbox.
So you need to check if there is a value in the database and then make your code come out like his.


if($rows['data_no'] != '') {
    $isChecked = ' checked="checked"';
} else {
    $isChecked = '';
echo "<input type=\\"checkbox\\" name=\\"dataid\\" value=\\"".$rowc["data_no"]."\\" $isChecked />"; echo $rowc['data_name'];

        echo "<br />";


foreach(range(1, 10) as $num){
  #determine if checked here, we're doing it randomly
  $isChecked = rand(0, 1);
    '<label for="%1$s">Box %2$d</label><input type="checkbox" id="%1$s" name="%1$s" %3$s />' . "\
    'box-' . $num,
    $isChecked ? 'checked="checked"' : ''

<label for="box-1">Box 1</label><input type="checkbox" id="box-1" name="box-1"  />
<label for="box-2">Box 2</label><input type="checkbox" id="box-2" name="box-2"  />
<label for="box-3">Box 3</label><input type="checkbox" id="box-3" name="box-3"  />
<label for="box-4">Box 4</label><input type="checkbox" id="box-4" name="box-4"  />
<label for="box-5">Box 5</label><input type="checkbox" id="box-5" name="box-5" checked="checked" />
<label for="box-6">Box 6</label><input type="checkbox" id="box-6" name="box-6" checked="checked" />
<label for="box-7">Box 7</label><input type="checkbox" id="box-7" name="box-7" checked="checked" />
<label for="box-8">Box 8</label><input type="checkbox" id="box-8" name="box-8"  />
<label for="box-9">Box 9</label><input type="checkbox" id="box-9" name="box-9"  />
<label for="box-10">Box 10</label><input type="checkbox" id="box-10" name="box-10"  />

I’d just suggest getting up to speed with HTML markup if you are not familiar with what some elements are used for and how to change their state (checked, unchecked, selected etc).

Excellent advice furicane. :wink: