denoteone Posted January 29, 2009 Share Posted January 29, 2009 I have a form that has check boxes, on submit I would like to save the check box array in a session variable. <form action="<?=$PHP_SELF?>" method="post"> <h5>Support Services</h5> <p>Customer wants the following service (choose all that apply)<br/> <INPUT TYPE=CHECKBOX NAME="servicelevel[]" value="Support Desk"> 1-800 Support Desk<br/> <INPUT TYPE=CHECKBOX NAME="servicelevel[]" value="Email Support">E-Mail Support<br/> <INPUT TYPE=CHECKBOX NAME="servicelevel[]" value="Remote Tech Support">Remote Technical Support<br/> <INPUT TYPE=CHECKBOX NAME="servicelevel[]" value="Depot Replacement">Depot Replacement<br/> <INPUT TYPE=CHECKBOX NAME="servicelevel[]" value="On Site Service">On-Site Service</p><br/> <input type="submit" name="submit" value="Next"> </form> this is what I have in my php $_SESSION['servcie_level'] = $_POST['servicelevel']; and then I would like to later show the value of $_SESSION['servcie_level'] so I have this on another page. Customer Wants the Following Services: <? foreach ( $_SESSION['service_level'] AS $name => $value ) { echo $value . "<br />"; } ?> but I am still getting an error. when it try's to show it on the screen. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/142977-solved-saving-checkbox-array-in-a-_session/ Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 but I am still getting an error. when it try's to show it on the screen. nothing jumps out at me as wrong...what is the error? Quote Link to comment https://forums.phpfreaks.com/topic/142977-solved-saving-checkbox-array-in-a-_session/#findComment-749689 Share on other sites More sharing options...
denoteone Posted January 29, 2009 Author Share Posted January 29, 2009 Warning: Invalid argument supplied for foreach() in /home/content/t/w/e/twenty20media/html/att_portal/Price_Check.php on line 60 Quote Link to comment https://forums.phpfreaks.com/topic/142977-solved-saving-checkbox-array-in-a-_session/#findComment-749696 Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 do you have session_start() at the top of both scripts? Quote Link to comment https://forums.phpfreaks.com/topic/142977-solved-saving-checkbox-array-in-a-_session/#findComment-749699 Share on other sites More sharing options...
denoteone Posted January 29, 2009 Author Share Posted January 29, 2009 yes I do. I echo about 20 other session var and thy work fine. here is line 60 foreach ( $_SESSION['service_level'] AS $name => $value ) Quote Link to comment https://forums.phpfreaks.com/topic/142977-solved-saving-checkbox-array-in-a-_session/#findComment-749701 Share on other sites More sharing options...
denoteone Posted January 29, 2009 Author Share Posted January 29, 2009 I think I got it. spelling error. in the session variable name. Quote Link to comment https://forums.phpfreaks.com/topic/142977-solved-saving-checkbox-array-in-a-_session/#findComment-749704 Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 I think I got it. spelling error. in the session variable name. i hate those Quote Link to comment https://forums.phpfreaks.com/topic/142977-solved-saving-checkbox-array-in-a-_session/#findComment-749705 Share on other sites More sharing options...
milesap Posted January 29, 2009 Share Posted January 29, 2009 PHP will not recognize the form name servicelevel[] as an array, but rather as a string. When you run the foreach loop, PHP requires an array, but finds a string instead. To trouble shoot this issue print the form input: print_r ($_POST['servicelevel']); Hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/142977-solved-saving-checkbox-array-in-a-_session/#findComment-749712 Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 PHP will not recognize the form name servicelevel[] as an array sure it will Quote Link to comment https://forums.phpfreaks.com/topic/142977-solved-saving-checkbox-array-in-a-_session/#findComment-749715 Share on other sites More sharing options...
ialsoagree Posted January 29, 2009 Share Posted January 29, 2009 Woops, never mind, I wasn't initializing the variable like you probably have! You can avoid the error (although in this case it was useful for debugging) by wrapping your foreach in an if statement: <?php if (is_array($SESSION['service_level'])) { foreach ( $_SESSION['service_level'] AS $name => $value ) { echo $value . "<br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142977-solved-saving-checkbox-array-in-a-_session/#findComment-749716 Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 I'm not sure if that's your error, here's something I noticed: <form action="<?=$PHP_SELF?>" method="post"> will produce: <form action="" method="post"> Try: <form action="<?php echo $PHP_SELF; ?>" method="post"> <?=$varname?> is shorthand of the <?php echo $varname; ?> Quote Link to comment https://forums.phpfreaks.com/topic/142977-solved-saving-checkbox-array-in-a-_session/#findComment-749718 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.