dadamssg87 Posted July 1, 2011 Share Posted July 1, 2011 i'm trying to check if a checkbox should be checked or not. I want the box to be checked on initial page load. And then after the form is submitted check whether or not the user had it check it or not and get it to remember. Oh and i have the checkbox's value to be "yes" but i've learned the $_POST value for this input doesn't even exist if the user submits the form without the box checked. This loads the form initially with it unchecked(not what i want) but it does remember what the user inputted if there is an error in the form, unchecked or checked so thats good. <?php if(isset($_POST['length']) && $_POST['length'] == "yes") { $values['length'] = TRUE; } elseif(!isset($_POST['length'])) { $values['length'] = FALSE; } else { $values['length'] = TRUE; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240907-_post-exists-problem/ Share on other sites More sharing options...
EdwinPaul Posted July 1, 2011 Share Posted July 1, 2011 A checkbox doesn't exist in the array $_POST if it isn't checked. If it is checked, it has value 1. Print your array: echo '<pre>'; print_r($_POST); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/240907-_post-exists-problem/#findComment-1237473 Share on other sites More sharing options...
Psycho Posted July 1, 2011 Share Posted July 1, 2011 If it is checked, it has value 1. Um, no, it has the value that you set in the "value" attribute of the input tag. Quote Link to comment https://forums.phpfreaks.com/topic/240907-_post-exists-problem/#findComment-1237474 Share on other sites More sharing options...
Pikachu2000 Posted July 1, 2011 Share Posted July 1, 2011 If it's checked, it should have the value in the value= attribute. Quote Link to comment https://forums.phpfreaks.com/topic/240907-_post-exists-problem/#findComment-1237475 Share on other sites More sharing options...
dadamssg87 Posted July 1, 2011 Author Share Posted July 1, 2011 so basically the value you set for it is worthless in php? You can't compare it to anything to make decisions? Quote Link to comment https://forums.phpfreaks.com/topic/240907-_post-exists-problem/#findComment-1237476 Share on other sites More sharing options...
EdwinPaul Posted July 1, 2011 Share Posted July 1, 2011 Okay, I stand corrected. It gets the value of your value= attribute. If you want it initially to be checked, add checked="checked" to your checkbox. Quote Link to comment https://forums.phpfreaks.com/topic/240907-_post-exists-problem/#findComment-1237479 Share on other sites More sharing options...
dadamssg87 Posted July 1, 2011 Author Share Posted July 1, 2011 yeah but it will remain checked if the user unchecks it and theres an error and the form gets redisplayed... Quote Link to comment https://forums.phpfreaks.com/topic/240907-_post-exists-problem/#findComment-1237481 Share on other sites More sharing options...
Pikachu2000 Posted July 1, 2011 Share Posted July 1, 2011 If I've understood what you're trying to achieve, here's a way that would work for you. <input type="checkbox" name="my_checkbox" value="Yes" <?php echo ( isset($_POST['my_checkbox']) && $_POST['my_checkbox'] == 'Yes' ) || strtolower($_SERVER['REQUEST_METHOD']) !== 'post' ? 'checked="checked"' : ''; ?>> Quote Link to comment https://forums.phpfreaks.com/topic/240907-_post-exists-problem/#findComment-1237484 Share on other sites More sharing options...
dadamssg87 Posted July 1, 2011 Author Share Posted July 1, 2011 thanks for attempt but didn't work. It loaded with it checked, great, but if i check it and submit the form and theres an error and the form gets redisplayed it gets unchecked Quote Link to comment https://forums.phpfreaks.com/topic/240907-_post-exists-problem/#findComment-1237490 Share on other sites More sharing options...
dadamssg87 Posted July 1, 2011 Author Share Posted July 1, 2011 i lied...you got it! thanks. I just had to change the 'Yes' to "yes". Quote Link to comment https://forums.phpfreaks.com/topic/240907-_post-exists-problem/#findComment-1237491 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.