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
https://forums.phpfreaks.com/topic/134179-is-there-a-better-way/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.