xpace Posted August 3, 2009 Share Posted August 3, 2009 Hi all, I have this code, created myself <?php $xbox = array(1,2,3,4,5); //this will be take from database - this is just for testing reset($xbox); if($_POST){ foreach($_POST['box'] as $key=>$value){ $_SESSION['storedbox'][$key] = $value; } foreach($_SESSION['storedbox'] as $key=>$value){ echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />'; //just for testing } } echo "<form action='' method='POST'>"; if(isset($_SESSION['storedbox'])){ foreach($xbox as $x){ for($i=0;$i<count($_SESSION['storedbox']);$i++){ if($_SESSION['storedbox'][$i] == $x){ $x_sel = "CHECKED"; }else{ $x_sel = NULL; } echo "box_".$x." <input type='checkbox' name='box[]' value='".$x."' ".$x_sel."><br />"; } } }else{ foreach($xbox as $x){ echo "box_".$x." <input type='checkbox' name='box[]' value='".$x."' ".$x_sel."><br />"; } } echo "<input type = 'submit' name='go' value=' go '></form>"; ?> and the purpose is that when you select something and hit GO button it should reappeared exactly how you selected it in the first place. it's ok when you select only one checkbox but when you do multiple it will multiply the checkboxes as well. you can try for yourself. I have no idea where the problem is though I know there is something I don't like in the code but not sure where thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/168636-solved-multiple-checkboxes-checked-on-reload/ Share on other sites More sharing options...
phpSensei Posted August 3, 2009 Share Posted August 3, 2009 you are looping your input field. If you select 3 checkboxes you will have also 3 check boxes added. Its inside the loop, take it out. Quote Link to comment https://forums.phpfreaks.com/topic/168636-solved-multiple-checkboxes-checked-on-reload/#findComment-889619 Share on other sites More sharing options...
xpace Posted August 3, 2009 Author Share Posted August 3, 2009 hey, I know I do, because I can see it on a page but I don't know which area to take out or move somewhere else- that's the part where I said that I have no idea where the problem is if you could just point me there that would really help thanks Quote Link to comment https://forums.phpfreaks.com/topic/168636-solved-multiple-checkboxes-checked-on-reload/#findComment-890149 Share on other sites More sharing options...
Psycho Posted August 3, 2009 Share Posted August 3, 2009 You're making it too complicated: <?php $xbox = array(1,2,3,4,5); //this will be take from database - this is just for testing reset($xbox); if($_POST['box']){ //Saved the submitted values to session variable $_SESSION['storedbox'] = $_POST['box']; foreach($_SESSION['storedbox'] as $key => $value){ echo "The value of \$_SESSION['{$key}'] is {$value}<br />\n"; //just for testing } } //Create the form echo "<form action=\"\" method=\"POST\">\n"; foreach($xbox as $x){ if (isset($_SESSION['storedbox']) && is_array($_SESSION['storedbox'])) { //Set the checked state for each option $checked = (in_array($x, $_SESSION['storedbox'])) ? ' checked="checked"' :''; } //Display label and checkbox echo "box_{$x}<input type=\"checkbox\" name=\"box[]\" value=\"{$x}\"{$checked} /><br />\n"; } echo "<button type=\"submit\"> go </button></form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/168636-solved-multiple-checkboxes-checked-on-reload/#findComment-890159 Share on other sites More sharing options...
xpace Posted August 4, 2009 Author Share Posted August 4, 2009 mate, you are an champion thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/168636-solved-multiple-checkboxes-checked-on-reload/#findComment-890333 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.