Jump to content

[SOLVED] Keep Checkboxes Checked Upon Form Submit


Omzy

Recommended Posts

Below is the code for my checkboxes, I have got a FOREACH loop which creates all the checkboxes.

 

 
foreach ($cat as $index => $value)
{
echo '<input type="checkbox" name="selection[]" value="'.$cat[$index][2].'" id="'.$index.'"/>'.$cat[$index][2].';
}

 

I've got some form processing going on that counts the number of checkboxes that are checked and if it is less than 3 then it redisplays the form with an error message. But I would like it to keep the checkboxes ticked too - how can this be acheived with the above code setup?

<?php
/**
*	@name checkBoxError() Checks the selected values when an error occurs. 
*	   
*	@param string $field  The name of the field being checked
*	@param string $value  The value to check against
*   @param string $key    The key of the particular checkbox item.
*/
function checkBoxError($fieldName, $value, $key)
{
	$theField = $_POST[$fieldName][$key];

	if($errors > 0 && $theField == $value)
	{
		return 'checked="checked"';
	}
}



$x = 0;
foreach ($cat as $index => $value)
{
echo '<input type="checkbox" name="selection['.$x.']" value="'.$cat[$index][2].'" id="'.$index.'" '.checkBoxError('selection',$cat[$index], $x).'/>'.$cat[$index][2].';
$x++;
}

?>

 

This is a function I use to do this. Since the checkboxes are an array, you have to check for it's key. The function takes the field name, the value of the field, and the key. It puts the fieldname and key together to pinpoint the individual checkboxes, and then it compares the value you pass to it to the value of the fieldname/key combo. If the 2 are the same, it checks the box, if not it does nothing.

 

You may need to modify the $error variable. It only needs to run if there are errors. So change the $error var to either the name of the error var you use or if you use an error array, change it to count($errorVarName) > 0

 

It works great. Lemme know if you have questions about it or if it is not working for ya.

 

 

 

Nate

Hi chronister,

 

Thanks for that. Unfortunately I couldn't get it to work though.

 

1) The echo line looks like it's got an apostrophe or escape character missing somewhere, I put in some extra apostrophe's so that it didn't complain but I'm not sure if this changed its operation.

 

2) I am using an error array called $errors. If I was to reference that array in the function do I not need to declare it global somewhere?

Yes, you need to add global $error inside the function.

 

The apostrophe's were for concatenation. Remove the apostrophe's you added and see if you get any errors. If there are no errors then the apostrophe's are not an issue.

 

I looked over it with color coding and it seems to be concatenated properly.

 

So remove the apostrophes you added, add global $error and see what you get on that.

 

Nate

QUALITY STUFF MATE!

 

I've got it working and it works a charm!

 

The reason why it didn't work at first was because I already had a $x variable in another FOR loop encasing the checkboxes. So I changed it to $j.

 

I changed the IF statement in the function to be:

 

if(count($GLOBALS['errors']) > 0 && $theField == $value)

 

Also the echo statement did actually have two apostrophe's missing at the end and you missed out the '[2]' in the function. Here is the working version:

 

echo '<input type="checkbox" name="selection['.$j.']" value="'.$cat[$index][2].'" id="'.$index.'" '.checkBoxError('selection',$cat[$index][2], $j).'/>'.$cat[$index][2].'';

 

Thanks for all your help on this, really appriciate it!

There is one slight issue though: It seems that if you make a valid selection (i.e. check 3 boxes in my case) and submit, and then hit the back button and check some more boxes, it will submit the page without any errors. Can you try this on your setup and see if you get the same?

Post your code....

 

Are you storing anything in sessions?? When you submit, and then hit back and submit again, the previous selections should go away. The only way the first set of options would be remembered is if your using sessions to store them.

 

Nate

Basically the code is as follows:

 

The $errors array that checks the number of boxes checked:

$max=3;
$errors=array();

if($_POST['selection'] == null || count($_POST['selection']) > $max)
  {
   array_push($errors, 'selection');
  }

 

The checkbox function:

 

function checkBoxError($fieldName, $value, $key)
{
$theField = $_POST[$fieldName][$key];

if(in_array('selection', $GLOBALS['errors']) && $theField == $value)
{
  return 'checked="checked"';
}
}

 

The code to display the error message:

 

if(in_array('selection', $values))
{
  if (count($_POST['selection']) > $max)
  {
   echo '<p><b class="red">You selected '.count($_POST['selection']).' categories. Please select up to '.$max.' categories only.</b></p>';
  }

 

The checkbox:

 

<input type="checkbox" name="selection['.$j.']" value="'.$cat[$index][2].'" id="'.$index.'" '.checkBoxError('selection',$cat[$index][2],$j).'/>'.$cat[$index][2].'';

The $cat array:

 

$cat=array(
'music-video'=>array('3', '0', 'Music & Video', 'Music & Video')
);

<input type="checkbox" name="selection['.$j.']" value="'.$cat[$index][2].'" id="'.$index.'" '.checkBoxError('selection',html_entity_decode($cat[$index][2]),$j).'/>'.$cat[$index][2].'';

 

 

Try that... I added the function html_entity_decode to the value, so that it can be written as & but inside the function, it will be &.

 

Dunno if that will work or not, but give it a go and see what you come up with.

 

Nate

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.