|
-
Jun 11th, 2008, 09:04 PM
#1
Thread Starter
Frenzied Member
Getting value of checkbox in form build from database
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:
Code:
<?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:
Code:
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.
-
Jun 11th, 2008, 09:14 PM
#2
Re: Getting value of checkbox in form build from database
If the checkbox is checked then its `value` attribute is sent to the server.
PHP Code:
<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.
-
Jun 11th, 2008, 10:02 PM
#3
Thread Starter
Frenzied Member
Re: Getting value of checkbox in form build from database
guess i'm doing sth wrong.
tried isset($_POST[active][$i]) without luck...will give it a break and check later. still asleep here
-
Jun 11th, 2008, 10:07 PM
#4
Re: Getting value of checkbox in form build from database
do
PHP Code:
print_r($_POST)
and see what is being sent.
-
Jun 20th, 2008, 10:48 PM
#5
Thread Starter
Frenzied Member
Re: Getting value of checkbox in form build from database
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:
Code:
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 } ?>
-
Jun 26th, 2008, 07:32 PM
#6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|