Click to See Complete Forum and Search --> : [RESOLVED] check box checked


raj_2006
05-29-2007, 02:52 AM
Hi

I want to keep the the checkboxes checked after the form is being posted.

Here is the code:

<input type="checkbox" name="food[<?=$mi;?>][]" value="Every Day">

is it possible in this case to keep checked the checkboxes.I have no idea

Please suggest

Thanks in advance

Raj.

bokeh
05-29-2007, 03:18 AM
Not with a dynamic array; you would need to keep track of the individual boxes somehow.

raj_2006
05-29-2007, 07:26 AM
Thanks for your reply.

you would need to keep track of the individual boxes somehow.

Can you plz give me a small example so that i can go through it and give it a try

Raj

bokeh
05-29-2007, 08:58 AM
Post your complete form and a dump of the $_POST array.

raj_2006
05-29-2007, 01:32 PM
Here is the form

<form action="" method="post">
<?$a=array("Meat","Fish","Eggs");
foreach($a as $km=>$mi)
{
?>
<tr bgcolor="#D2E9FF">
<td> <font color="#003366" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<?=$mi;?>
</font></td>
<td bgcolor="#D2E9FF" align="center"><font color="#003366" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<input type="checkbox" name="food[<?=$mi;?>][]" value="Every Day">
</font></td>
<td bgcolor="#D2E9FF" align="center"><font color="#003366" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<input type="checkbox" name="food[<?=$mi;?>][]" value="Every Week">
</font></td>
<td bgcolor="#D2E9FF" align="center"><font color="#003366" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<input type="checkbox" name="food[<?=$mi;?>][]" value="Every Year">
</font></td>
<td bgcolor="#D2E9FF" align="center"><font color="#003366" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<input type="checkbox" name="food[<?=$mi;?>][]" value="Never">
</font></td>
</tr>
<?}?>

and here is the output of $_POST array

Array([food] => Array ( [Meat] => Array ( [0] => Every Day ) [Fish] => Array ( [0] => Every Day ) [Eggs] => Array ( [0] => Every Day ) ))

I am fetching the values of the food array in this way:

if($_POST['food']!="")
{
foreach($_POST['food'] as $fk=>$fv)
{
foreach($fv as $fv1)
{
$con .=$fk."=".$fv1."<br><br>";
}
}
}

bokeh
05-29-2007, 03:02 PM
I don't know why you are using a table and all that deprecated mark-up but each to his own:<?php

$foods = array("Meat","Fish","Eggs");
$regularity = array('Every Day','Every Week','Every Year','Never');

echo '<form action="" method="post">'."\n".
'<table>'."\n";

foreach($foods as $food)
{
echo '<tr bgcolor="#D2E9FF">'."\n".
'<td> <font color="#003366" size="2" face="Verdana, Arial, Helvetica, sans-serif">'. $food .'</font></td>'."\n";
foreach($regularity as $period)
{
echo '<td bgcolor="#D2E9FF" align="center"><font color="#003366" size="2" face="Verdana, Arial, Helvetica, sans-serif"> '."\n".
$period .'<input type="radio" name="food['. $food .']" value="'. $period .'"'. (@$_POST['food'][$food] == $period?' checked="checked"':'') .'> '."\n".
'</font>'."\n".
'</td>'."\n";
}
echo '</tr>'."\n";
}

# etc
echo '<table>'."\n".
'<input type="submit" value="submit">'."\n";
'</form>'."\n";

#view output
if($_POST)
{
echo "<pre>\n";
print_r($_POST['food']);
echo "</pre>\n";
}

?>

raj_2006
05-29-2007, 04:21 PM
Hi

Thanks for your help....I have tested it but the problem is that I am using checkbox instead of radio button and I have changed the code a bit as I am getting the last element of the array everytime.

name="food['. $food .'][]" and value="'. $period .'"

and this is the output

Array
(
[Meat] => Array
(
[0] => Every Day
[1] => Every Year
)

[Fish] => Array
(
[0] => Every Day
)

[Eggs] => Array
(
[0] => Every Day
[1] => Every Year
)

)

But doing as this I am losing the checked mark......

bokeh
05-29-2007, 05:23 PM
Every Day, Every Week, Every Year, Never

You need radio buttons for those options as only one should be selectable. It doesn't make sense that someone who never eats fish can also eat it once a week.

raj_2006
05-31-2007, 08:45 AM
Yes....I also think that multiple checkbox will not work in this issue....

OK.......I should say one thing.....Honestly....:D

I take time to understand your code.....its small but really nice....not to write so many lines....like 4 lines will work of 10 lines if you write....:)

ok once again thanks to you for your kind contribution.

Raj.