hbradshaw Posted May 12, 2007 Share Posted May 12, 2007 Hello: I'm trying to get a form with checkboxes to be sticky and to remember what the person checked. I am using sessions and when I echo the results of the checkboxes the correct result appears. Thus far, all the code I've found makes all the boxes checked. After the person clicks submit, I only want the boxes checked that the person checked. Can someone help me out? Thank you in advance. This is the code that I have. <input type="checkbox" name="availability[]" id="availability" value="Weekday Mornings"<?php echo isset ($_SESSION['availability'] && $_SESSION['availability'] == 'Weekday Mornings' ? ' checked="checked"' : ''; ?> />Weekday mornings<br> <input type="checkbox" name="availability[]" id="availability" value="Weekday Afternoons"<?php echo isset ($_SESSION['availability'] && $_SESSION['availability'] == 'Weekday Mornings' ? ' checked="checked"' : ''; ?> />Weekday Afternoons<br> Quote Link to comment https://forums.phpfreaks.com/topic/51057-checkbox-form-field-making-sticky-and-remembering-input/ Share on other sites More sharing options...
gandaliter Posted May 12, 2007 Share Posted May 12, 2007 Hi, in the code you entered: <input type="checkbox" name="availability[]" id="availability" value="Weekday Mornings"<?php echo isset ($_SESSION['availability'] && $_SESSION['availability'] == 'Weekday Mornings' ? ' checked="checked"' : ''; ?> />Weekday mornings<br> <input type="checkbox" name="availability[]" id="availability" value="Weekday Afternoons"<?php echo isset ($_SESSION['availability'] && $_SESSION['availability'] == 'Weekday Mornings' ? ' checked="checked"' : ''; ?> />Weekday Afternoons<br> you have used the check value 'Weekday Mornings' for both the mornings and afternoons checkboxes, try this instead: <input type="checkbox" name="availability[]" id="availability" value="Weekday Mornings"<?php echo isset ($_SESSION['availability'] && $_SESSION['availability'] == 'Weekday Mornings' ? ' checked="checked"' : ''; ?> />Weekday mornings<br> <input type="checkbox" name="availability[]" id="availability" value="Weekday Afternoons"<?php echo isset ($_SESSION['availability'] && $_SESSION['availability'] == 'Weekday Afternoons' ? ' checked="checked"' : ''; ?> />Weekday Afternoons<br> Is that it, or was that just a typo on the post? Also, I would recommend using radio buttons rather than check boxes if you only want one checked at a time. Or, if you want to be able to have them both checked, you might want to use more than one session variable for them, but I don't know the context so I could be wrong. Quote Link to comment https://forums.phpfreaks.com/topic/51057-checkbox-form-field-making-sticky-and-remembering-input/#findComment-251335 Share on other sites More sharing options...
soycharliente Posted May 12, 2007 Share Posted May 12, 2007 You have no closing parenthesis on your isset() function. Maybe that is also part of the problem. Quote Link to comment https://forums.phpfreaks.com/topic/51057-checkbox-form-field-making-sticky-and-remembering-input/#findComment-251391 Share on other sites More sharing options...
Psycho Posted May 12, 2007 Share Posted May 12, 2007 OK, there are several problems here: 1. You are using availability[] as the name for both fields. I'm not sure how you are setting you session values, but you can't reference two independant values using the same variable!, i.e. $_SESSION['availability'] can't be two different things. I would think you would make $_SESSION['availability'] an array since it can have multiple values. So you should be using in_array(). 2. You are using the same id for both fields. This probably isn't causing the problem you are having, but it is incorrect. 3. Your PHP is malformed. It should be like this: echo ( isset($_SESSION['availability']) && in_array('Weekday Mornings' , $_SESSION['availability']) )? ' checked="checked"' : ''; Quote Link to comment https://forums.phpfreaks.com/topic/51057-checkbox-form-field-making-sticky-and-remembering-input/#findComment-251400 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.