Jump to content

Is there a better way?


goocharlton

Recommended Posts

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.