tags:

views:

19

answers:

3

Hello guys,

I know this should be an easy one, but I'm failing to make it work. I have the following form:

<form action="" method="post">
<input type="checkbox" name="blades[]" value="2" />Blade AM-01 <br />
<input type="checkbox" name="blades[]" value="5" />Blade AM-02 <br />
<input type="checkbox" name="blades[]" value="10" />Blade KT-24 <br />
<input type="checkbox" name="blades[]" value="1" />Blade FR-98 <br />
<input type="checkbox" name="blades[]" value="66" />Blade PR-11 <br />
</form>

After submit I want to display the form again and to check the checkboxes that the user has selected before submission. My language of choice is PHP.

Thanks.

A: 

If I select the first three checkboxes, then $_POST will be:

Array ( [blades] => Array ( [0] => 2 [1] => 5 [2] => 10 ) )

If I select the first, third and fifth checkboxes:

Array ( [blades] => Array ( [0] => 2 [1] => 10 [2] => 66 ) )

This was done with:

print_r($_POST);

As you can see, $_POST['blades'] is an array with each selected value.

My full testing code was:

<form action="" method="post">
<input type="checkbox" name="blades[]" value="2" />Blade AM-01 <br />
<input type="checkbox" name="blades[]" value="5" />Blade AM-02 <br />
<input type="checkbox" name="blades[]" value="10" />Blade KT-24 <br />
<input type="checkbox" name="blades[]" value="1" />Blade FR-98 <br />
<input type="checkbox" name="blades[]" value="66" />Blade PR-11 <br />
<input type="submit">
</form>
<?php print_r($_POST); ?>
Delan Azabani
A: 
<?php $blades = $_POST["blades"]?>
<form action="" method="post">
<input type="checkbox" name="blades[]" <?php if($blades[0]==2):?>checked="checked"<?php endif?> value="2" />Blade AM-01 <br />
<input type="checkbox" name="blades[]" <?php if($blades[1]==5):?>checked="checked"<?php endif?> value="5" />Blade AM-02 <br />
<input type="checkbox" name="blades[]" <?php if($blades[2]==10):?>checked="checked"<?php endif?> value="10" />Blade KT-24 <br />
<input type="checkbox" name="blades[]" <?php if($blades[3]==1):?>checked="checked"<?php endif?> value="1" />Blade FR-98 <br />
<input type="checkbox" name="blades[]" <?php if($blades[4]==66):?>checked="checked"<?php endif?> value="66" />Blade PR-11 <br />
</form>
dekomote
Ah, but... `$blades[n]` may not be what you expect. `$blades[3]` is only `1` if I've selected all three checkboxes before it. If I've selected the first and fourth only, then `$blades[1]` is in fact `1`, because the second and third checkboxes, which are unchecked, do not have a place in the array.
Delan Azabani
Yeah. Didn't think of that.
dekomote
+1  A: 

Something like this should work:

<form action="" method="post">
<input type="checkbox" name="blades[]" value="2" <?=(in_array("2", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade AM-01 <br />
<input type="checkbox" name="blades[]" value="5" <?=(in_array("5", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade AM-02 <br />
<input type="checkbox" name="blades[]" value="10" <?=(in_array("10", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade KT-24 <br />
<input type="checkbox" name="blades[]" value="1" <?=(in_array("1", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade FR-98 <br />
<input type="checkbox" name="blades[]" value="66" <?=(in_array("66", $_POST['blades']) ? "checked='checked'" : "") ?> />Blade PR-11 <br />
</form>
xil3