PDA

Click to See Complete Forum and Search --> : Getting value of checkbox in form build from database


StrangerInBeijing
Jun 11th, 2008, 09:04 PM
I got this page I want to use to edit records in the database. All text fields, and one bool (tinyint(1)) field to indicate whether the record is active or not.

I build up a table of records from the database like this:
<?php
$count = $results->num_rows;
while($row = $results->fetch_object()) {
?>
<tr>
<td>
<input type="hidden" name="_count" value="<?php echo $count ?>"/>
<input name="id[]" type="hidden" id="id" value="<?php echo $row->id; ?>" />
</td>
<td>
<input name="lang_code[]" type="text" id="lang_code" value="<?php echo $row->lang_code; ?>" />
</td>
<td>
<input name="name_en[]" type="text" id="name_en" value="<?php echo $row->name_en; ?>" />
</td>
<td>
<input name="name_native[]" type="text" id="name_native" value="<?php echo $row->name_native; ?>" />
</td>
<td>
<input type="checkbox" <?php if($row->active) echo "checked='checked'"; ?> value="<?php echo $row->active; ?>" name="active[]" id="active" />
</td>
</tr>
<?php } ?>
and at the top of the page run the following if the page was posted:
for($i=0;$i< $_POST['_count'];$i++){
$id=$_POST[id][$i];
$lang_code=$_POST[lang_code][$i];
$name_en=$_POST[name_en][$i];
$name_native=$_POST[name_native][$i];
$active=$_POST[active][$i];
update_language($id,$lang_code,$name_en,$name_native,$active);
}

Everything works fine, it's just I cannot get whether the check box ($active) was checked or not.

penagate
Jun 11th, 2008, 09:14 PM
If the checkbox is checked then its `value` attribute is sent to the server.

<input type="checkbox" name="test" value="Bananas" />
...
<?php
if (isset($_POST['test']) && $_POST['test'] == 'Bananas')
# ...
?>


If it is not checked then its value is blank.

StrangerInBeijing
Jun 11th, 2008, 10:02 PM
guess i'm doing sth wrong.
tried isset($_POST[active][$i]) without luck...will give it a break and check later. still asleep here

penagate
Jun 11th, 2008, 10:07 PM
do
print_r($_POST)

and see what is being sent.

StrangerInBeijing
Jun 20th, 2008, 10:48 PM
I did the print_r($_POST) thing, and realized I don't see "active" in there.

Then did print_r($_POST[language_code]); and get the values as it should be send.

print_r($_POST[active]); returned nothing. So the array for the $active[] checkbox are not send.

So for text fields the following work fine, but not for checkbox. Can anyone think what is wrong with the following:

while($row = $results->fetch_object()) { ?>
<tr>
<td>
<input name="id[]" type="hidden" id="id" value="<?php echo $row->id; ?>" />
</td>
<td>
<input name="language_code[]" type="text" id="language_code" value="<?php echo $row->language_code; ?>" />
</td>
<td>
<input name="language_english[]" type="text" id="language_english" value="<?php echo $row->language_english; ?>" />
</td>
<td>
<input name="language_native[]" type="text" id="language_native" value="<?php echo $row->language_native; ?>" />
</td>
<td>
<input type="checkbox" <?php if($row->active) echo("checked='checked'"); ?> name="active[]" id="active" value="1" />
</td>
</tr>
<?php } ?>

manavo11
Jun 26th, 2008, 07:32 PM
If it is not checked, i think it doesn't get sent at all (not just a blank value). isset should return false.