goocharlton Posted November 25, 2008 Share Posted November 25, 2008 I am referring to the following script. <?php $specialties = array("Variable 1", "Variable 2"); $i = 0; extract($_POST['specialties']); foreach($specialties as $value) { if (isset($_POST['specialties'])) { $checked = ''; if ($specialties[$i] == $value) { $checked = ' checked'; } } echo "<tr><td><input name='specialties[]' type='checkbox' value='$value'$checked /></td><td>$value $i</td></tr>"; ++$i; } ?> I am creating a list of checkboxes for my form using an array. I have setup the script to check if all fields are filled in before it submits itself. If the validation doesn't pass then the script reloads the form with the field filled with the details the user had entered. I am trying to also make it so the checkboxes that the user had selected before validation are still checked after they don't pass validation. I have tried doing so using: <?php if ($specialties[$i] == $value) { $checked = ' checked'; } ?> ...but I am unable to get $specialties[$i] to work correctly. I have tried $specialties[0] and the script works fine for the first checkbox which is of course what I want for the all the checkboxes. That is why I have tried using $i but not successfully. Is there a better way of doing this? What am I doing wrong? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.