TomT Posted May 19, 2012 Share Posted May 19, 2012 Hi I'm hoping someone can point me in the right direction. I'm trying to create a single page that contains a couple of checkboxes. When the first checkbox is checked I want to set a session. When the second chechbox is checked I want to set a different session. When the page submits ( to itself ) I'd like the checkboxes to reflect if they are checked or not. I can sort of do that part,but I'm having major issues with unsetting the sessions and updating the checkboxes to reflect this. Does anyone have a simple example on how to do this. Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/262769-remember-checkbox-state/ Share on other sites More sharing options...
trq Posted May 19, 2012 Share Posted May 19, 2012 The process is simple: <input type="checkbox" value="foo"<?php isset($somevar) && $somevar == 'foo' ? ' selected="selected"' : ''; ?>> If $somevar is defined and equals 'foo' your checkbox will be selected. Quote Link to comment https://forums.phpfreaks.com/topic/262769-remember-checkbox-state/#findComment-1346792 Share on other sites More sharing options...
Barand Posted May 19, 2012 Share Posted May 19, 2012 Another thing to remember with checkboxes is that only checked values are sent. <input type=hidden name='cb1' value='0'> <input type=checkbox name='cb1' value='1'> Quote Link to comment https://forums.phpfreaks.com/topic/262769-remember-checkbox-state/#findComment-1346796 Share on other sites More sharing options...
TomT Posted May 20, 2012 Author Share Posted May 20, 2012 Thanks for the replies. I can create the checkbox and get the value from it.. The problem I have is more with setting and unsetting the session and also showing the state of the checkbox depending how the session is set. Can someone help with this ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/262769-remember-checkbox-state/#findComment-1347121 Share on other sites More sharing options...
TomT Posted May 22, 2012 Author Share Posted May 22, 2012 Hi can anyone help with this ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/262769-remember-checkbox-state/#findComment-1347759 Share on other sites More sharing options...
Barand Posted May 22, 2012 Share Posted May 22, 2012 $cb1_checked = isset($_SESSION['cb1']) && $_SESSION['cb1']==1 ? 'checked' : ''; $cb2_checked = isset($_SESSION['cb2']) && $_SESSION['cb2']==1 ? 'checked' : ''; echo "input type='checkbox' name = 'cb1' value='1' $cb1_checked /> CB1 echo "input type='checkbox' name = 'cb2' value='1' $cb2_checked /> CB2 Quote Link to comment https://forums.phpfreaks.com/topic/262769-remember-checkbox-state/#findComment-1347771 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.