Omzy Posted December 21, 2008 Share Posted December 21, 2008 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? Quote Link to comment Share on other sites More sharing options...
Omzy Posted December 21, 2008 Author Share Posted December 21, 2008 So can anybody help with this? It is rather tricky this one! Quote Link to comment Share on other sites More sharing options...
chronister Posted December 21, 2008 Share Posted December 21, 2008 <?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 Quote Link to comment Share on other sites More sharing options...
Omzy Posted December 21, 2008 Author Share Posted December 21, 2008 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? Quote Link to comment Share on other sites More sharing options...
chronister Posted December 21, 2008 Share Posted December 21, 2008 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 Quote Link to comment Share on other sites More sharing options...
Omzy Posted December 22, 2008 Author Share Posted December 22, 2008 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! Quote Link to comment Share on other sites More sharing options...
Omzy Posted December 22, 2008 Author Share Posted December 22, 2008 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? Quote Link to comment Share on other sites More sharing options...
chronister Posted December 22, 2008 Share Posted December 22, 2008 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 Quote Link to comment Share on other sites More sharing options...
Omzy Posted December 22, 2008 Author Share Posted December 22, 2008 Nope not using sessions. Actually I think it's working now, I have moved the $j=0 variable into the outermost FOR loop (it's a messy FOR-FOREACH loop), everything seems to be fine now. Thanks! Quote Link to comment Share on other sites More sharing options...
Omzy Posted December 22, 2008 Author Share Posted December 22, 2008 chronister - it seems that it doesn't work with checkboxes who's value includes '&' Not sure if its the '&' or the ';' it is not liking. Can you investigate? Quote Link to comment Share on other sites More sharing options...
chronister Posted December 22, 2008 Share Posted December 22, 2008 When the value is posted, it is probably a single & rather than &. You will probably need to compare it to & instead of &. Nate Quote Link to comment Share on other sites More sharing options...
Omzy Posted December 22, 2008 Author Share Posted December 22, 2008 Hi, Yes that actually works. But if I use '&' rather than '&' it doesn't validate as XHTML 1.0 Strict. And I need to use this doctype for this project. Any more ideas? Quote Link to comment Share on other sites More sharing options...
chronister Posted December 22, 2008 Share Posted December 22, 2008 Post your code in full so I can look at it. Nate Quote Link to comment Share on other sites More sharing options...
Omzy Posted December 22, 2008 Author Share Posted December 22, 2008 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') ); Quote Link to comment Share on other sites More sharing options...
chronister Posted December 22, 2008 Share Posted December 22, 2008 <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 Quote Link to comment Share on other sites More sharing options...
Omzy Posted December 23, 2008 Author Share Posted December 23, 2008 Wow that has worked! I think I finally have a working solution and this can be marked as 'solved' for the final time! Thanks once again! ;-) 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.