rbragg Posted May 7, 2007 Share Posted May 7, 2007 I have a group of checkboxes that I would like to be sticky. I want the checkboxes that the user has checked to remain checked upon returning to the page from a validation error. <?php <input name="cbUnit[]" type="checkbox" value="Talon">Talon<br> <input name="cbUnit[]" type="checkbox" value="Star">Star<br> <input name="cbUnit[]" type="checkbox" value="Cater">Cater<br> <input name="cbUnit[]" type="checkbox" value="Landrum">Landrum<br> ?> I've tried this: <?php $sticky_cbUnit = ( (isset($_SESSION['cbUnit'])) && ($_SESSION['cbUnit'] == 'Talon') )?' checked="checked" ':' '; echo '<input type="checkbox" ' . $sticky_cbUnit . ' name="cbUnit[]" value="Talon" />'; ?>Talon<br> I'm guessing the array [] is giving me the problem but it WILL be an array so I need it. ??? Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/ Share on other sites More sharing options...
papaface Posted May 7, 2007 Share Posted May 7, 2007 Do you have session_start(); at the top of the page. Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247210 Share on other sites More sharing options...
rbragg Posted May 7, 2007 Author Share Posted May 7, 2007 Oh yes. All of the other form elements on the page (droplists, radiobuttons, textfields) are functioning and sticky EXCEPT for the checkboxes. *EDITED* to add "except for the checkboxes" because someone erroneously marked this topic as solved. Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247216 Share on other sites More sharing options...
MadTechie Posted May 7, 2007 Share Posted May 7, 2007 <?php <input name="cbUnit[]" type="checkbox" value="Talon">Talon<br> <input name="cbUnit[]" type="checkbox" value="Star">Star<br> <input name="cbUnit[]" type="checkbox" value="Cater">Cater<br> <input name="cbUnit[]" type="checkbox" value="Landrum">Landrum<br> ?> <?php session_start(); $_SESSION['cbUnit'] = $_POST['cbUnit']; $sticky_cbUnit = ( (in_array($_SESSION['cbUnit'], 'Talon') )?' checked="checked" ':' '; //Note: the comparison is done in a case-sensitive manner. echo '<input type="checkbox" ' . $sticky_cbUnit . ' name="cbUnit[]" value="Talon" />'; ?> Talon<br> EDIT: Click Solved Bottom left Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247225 Share on other sites More sharing options...
rbragg Posted May 7, 2007 Author Share Posted May 7, 2007 Thanks for your help. I have this: <?php $sticky_cbUnit = (in_array($_SESSION['cbUnit'],'Talon') )?' checked="checked" ':' '; echo '<input type="checkbox" ' . $sticky_cbUnit . ' name="cbUnit[]" value="Talon" />'; ?>Talon<br> After receiving a parse error I removed an opening "(" in front of in_array. Now, I get this error: Warning: in_array(): Wrong datatype for second argument in /food_test/ea_form.php on line 132 Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247228 Share on other sites More sharing options...
rbragg Posted May 7, 2007 Author Share Posted May 7, 2007 I'd imagine the error is because the second parameter, 'Talon', is not seen as an array. I tried to store the values in an array: $unitNames = array('Talon', 'Star', 'Cater', 'Landrum'); Then, <?php $sticky_cbUnit = (in_array($_SESSION['cbUnit'], $unitNames) )?' checked="checked" ':' '; echo '<input type="checkbox" ' . $sticky_cbUnit . ' name="cbUnit[]" value="Talon" />'; ?>Talon<br> The datatype error goes away but I don't have sticky checkboxes. Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247285 Share on other sites More sharing options...
boo_lolly Posted May 7, 2007 Share Posted May 7, 2007 the values should be held in your $_POST array, not your $_SESSION array. Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247292 Share on other sites More sharing options...
rbragg Posted May 7, 2007 Author Share Posted May 7, 2007 Hi boo_lolly! I tried this: <?php $sticky_cbUnit = (in_array('cbUnit', $unitNames) )?' checked="checked" ':' '; echo '<input type="checkbox" ' . $sticky_cbUnit . ' name="cbUnit[]" value="Talon" />'; ?>Talon<br> The boxes still aren't sticky. I also tried 'cbUnit[]'. Some background info - this page submits to itself and stores the values in the $_SESSION array. <?php session_start(); if( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['confirm']) ) # only if form is submitted { foreach ($_POST as $key => $value) # put the values into session variables { if ($key != "confirm") { $_SESSION[$key] = strip_tags($value); } } include 'shared/validate.php'; } ?> If there is an error found at validate.php then it will return. My radiobuttons use the $_SESSION array and are successfully sticky: <?php $sticky_rbStart = ( (isset($_SESSION['rbStart'])) && ($_SESSION['rbStart'] == 'Spring') )?' checked="checked" ':' '; echo '<input name="rbStart" id="rbStart" type="radio" value="Spring" ' . $sticky_rbStart . '/> '; ?>Spring Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247297 Share on other sites More sharing options...
MadTechie Posted May 7, 2007 Share Posted May 7, 2007 please post your full code, as sections are missing Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247317 Share on other sites More sharing options...
rbragg Posted May 7, 2007 Author Share Posted May 7, 2007 Everything is posted above except for the other form elements, which are unrelated to the checkboxes. Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247321 Share on other sites More sharing options...
MadTechie Posted May 7, 2007 Share Posted May 7, 2007 did you try my last suggestion? as you seams to of just used part of it (which would fail) Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247324 Share on other sites More sharing options...
rbragg Posted May 7, 2007 Author Share Posted May 7, 2007 Yes. I would think moving the values from the POST to the SESSION array is redundant since I am already doing that upon submission (using $key => value). Right? Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247329 Share on other sites More sharing options...
MadTechie Posted May 7, 2007 Share Posted May 7, 2007 Not exactly.. basically i have lost track of what your doing.. Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247349 Share on other sites More sharing options...
papaface Posted May 7, 2007 Share Posted May 7, 2007 Ignore this post. Won't work. Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247357 Share on other sites More sharing options...
boo_lolly Posted May 7, 2007 Share Posted May 7, 2007 i think your method is wrong. it should be something like this: <?php $checkboxValues = array('Talon', 'Star', 'Cater', 'Landrum'); foreach($checkboxValues as $key => $val){ echo "<input type=\"checkbox\" name=\"cbUnit[]\" value=\"{$val}\""; foreach($_POST['cbUnit'] as $index => $checked){ echo (($val == $checked) ? (' checked="yes"') : ('')); } echo ">{$val}\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247372 Share on other sites More sharing options...
MadTechie Posted May 7, 2007 Share Posted May 7, 2007 looks good boo_lolly Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247379 Share on other sites More sharing options...
rbragg Posted May 7, 2007 Author Share Posted May 7, 2007 Ok thanks for sticking with me guys. I think I understand this method and now I have this: <?php $cbUnitName = array('Talon', 'Star', 'Cater', 'Landrum'); foreach($cbUnitName as $key => $unitName) { echo "<input type='checkbox' name='cbUnit[]' value='" . $unitName . "'"; echo "unit name: " . $unitName . "\n"; # added to see if $unitName is being set foreach($_POST['cbUnit'] as $index => $checkedUnit) { echo (($unitName == $checkedUnit) ? (' checked="yes"') : ('')); } echo ">" . $unitName . "\n"; } ?> For every item in the array, I get: Warning: Invalid argument supplied for foreach() in /food_test/ea_form.php on line 139 >Talon Line 139 is the second foreach construct. When I try to echo the $unitName I don't get anything ... not even the "unit name: ". So, I'm guessing that the first foreach is not functioning? I did also copy what you had here directly and had the same warning. Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247451 Share on other sites More sharing options...
sasa Posted May 7, 2007 Share Posted May 7, 2007 try <?php $cbUnitName = array('Talon', 'Star', 'Cater', 'Landrum'); foreach($cbUnitName as $key => $unitName) { echo "<input type='checkbox' name='cbUnit[]' value='" . $unitName . "'"; echo "unit name: " . $unitName . "\n"; # added to see if $unitName is being set if (!isset($_POST['cbUnit'])) $_POST['cbUnit'] = array(); foreach($_POST['cbUnit'] as $index => $checkedUnit) { echo (($unitName == $checkedUnit) ? (' checked="yes"') : ('')); } echo ">" . $unitName . "\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247466 Share on other sites More sharing options...
rbragg Posted May 7, 2007 Author Share Posted May 7, 2007 Thank you sasa! This worked. I know you put a condition in there to see if the box was checked but please explain what it does specifically. I'll then mark this thread as solved. Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247497 Share on other sites More sharing options...
sasa Posted May 7, 2007 Share Posted May 7, 2007 i chek if is not set $_POST['cbUnit'] and if not exist (nothing is posted) i set $_POST['cbUnit'] to empty array you can change this code in diferen way like this if (isset($_POST['cbUnit'])) { foreach($_POST['cbUnit'] as $index => $checkedUnit) { echo (($unitName == $checkedUnit) ? (' checked="yes"') : ('')); } } Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247502 Share on other sites More sharing options...
rbragg Posted May 7, 2007 Author Share Posted May 7, 2007 Thanks very much. Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247509 Share on other sites More sharing options...
rbragg Posted May 8, 2007 Author Share Posted May 8, 2007 I have another problem pertaining to this thread. <?php $cbUnitName = array(''Talon', 'Star', 'Cater', 'Landrum'); foreach($cbUnitName as $key => $unitName) { echo "<input type='checkbox' name='cbUnit[]' value='" . $unitName . "'"; if ( !isset($_POST['cbUnit']) ) $_POST['cbUnit'] = array(); { foreach($_POST['cbUnit'] as $index => $checkedUnit) { echo (($unitName == $checkedUnit) ? (' checked="yes"') : (''));; } echo ">" . $unitName . "<br>"; } } ?> When I submit the form, cbUnit is stored in the Sessions array. However, I can't get the values (if there are any) to echo on the next page. I move to that next page and print what is stored in the Sessions array. For cbUnit I get: [cbUnit] => Array In the space where I would like to echo the values I do this: <?php $cbUnit = $_SESSION['cbUnit']; foreach($cbUnit as $key => $unit) { echo $unit . "<br>"; }?> I get the "Warning: Invalid argument supplied for foreach()" error. Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-248108 Share on other sites More sharing options...
boo_lolly Posted May 8, 2007 Share Posted May 8, 2007 that probably means that $_SESSION['cbUnit']. you can confirm this by putting: echo "<pre>". print_r($_SESSION, true) ."</pre>\n"; in your page. Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-248213 Share on other sites More sharing options...
rbragg Posted May 8, 2007 Author Share Posted May 8, 2007 Hi! This still gives me: [cbUnit] => Array and: Warning: Invalid argument supplied for foreach() Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-248255 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 echo "<pre>". print_r($_SESSION['cbUnit'], true) ."</pre>\n"; That should print out the array of cbUnit. Another fun way to do it might be <?php if (is_array($_SESSION)) { foreach ($_SESSION as $key => $val) { if (is_array($val)) { print $key . " => " . print_r($val, true) . "<br />"; }else { print $key . " => " . $val . "<br />"; } } } ?> Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-248295 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.