Forum Moderators: coopster

Message Too Old, No Replies

Checkboxes Checked

         

sfast

9:21 pm on Nov 28, 2007 (gmt 0)

10+ Year Member



I have a list of addresses with checkboxes.
WHen I submit the form after checking some checkboxes, those checkboxes should remain checked. I tried googing but it showed me only with static values and not with the ones retrieved from database.

if(isset($_GET['address'])) {

foreach( ($_GET['address']) as $value){

echo $value; }
}

Select data from table ;
$address_no = $row[0] ;

while ($row = mysql_fetch_array($result)) {

<input type="checkbox" name="address[]" value="<? echo $address_no;?>" />
}

PHP_Chimp

9:27 pm on Nov 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To check a checkbox you need either -

HTML
<input type="checkbox" name="address[]" value="<? echo $address_no;?>" checked >
XHTML
<input type="checkbox" name="address[]" value="<? echo $address_no;?>" checked="yes"/>

I dont know if that was your problem or not, but may help.

<edit>
You may need to check the html version...I havent used that version for a good few years so may well be wrong. However as you were closing the tag I guess you are xhtml'ing :)

[edited by: PHP_Chimp at 9:31 pm (utc) on Nov. 28, 2007]

sfast

9:59 pm on Nov 28, 2007 (gmt 0)

10+ Year Member



Thanks for writing, PHP_Chimp.

But want to check them only when I submit the form, depending on if they were checked before submitting.

PHP_Chimp

10:46 pm on Nov 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok im still not completely sure what you are after (its late here, so I apologise if im a little slow).
What I think you are asking for is for a list of just those check boxes that are checked.
Only boxes that are checked will show up in the $_GET¦POST array. So if you know that there are values of some_value, some_value_1 and some_value_2 and only some_value shows up in the $_GET array then the others were not checked.

Try what I have below to see what I mean about only the checked versions appearing in the $_GET array.
There is also the for loop in there that you could use to loop through the ['address'] array to find checked values.

test.php
<?php
if(isset($_GET['address'])) {
echo '<pre>';
print_r($_GET);
echo '</pre>';
$length = count($_GET['address']);
for ($i=0; $i<$length; $i++) {
if($_GET['address'][$i] == 'Some_value'){
echo '<input type="checkbox" name="address[]" value="some_value" checked="checked" />';
}
}
}
else {
?>
<html>
<head>
</head>
<body>
<form method="get" action="test.php">
<input type="checkbox" name="address[]" value="Some_value" checked="checked" />
<input type="checkbox" name="address[]" value="Some_value_1" />
<input type="checkbox" name="address[]" value="Some_value_2" />
<input type="submit" />
</form>
</body>
</html>
<?php
}
?>

[edited by: PHP_Chimp at 10:47 pm (utc) on Nov. 28, 2007]