unsider Posted August 5, 2008 Share Posted August 5, 2008 Is this method similar to <select> forms? I'm mainly asking because I've never worked with checkboxes, and I googled some articles, but couldn't find exactly what I was looking for. So I'm trying to retain 1 checkbox value that has been checked after a refresh. True or False. If it's similar to <select> here's some code to make it easier on you. <?php $selected = array(); $cat_values = 20; for ($i = 0; $i < $cat_values; $i++) { if ($i == $_POST['cat']) $selected[$i] = 'selected'; else $selected[$i] = ''; } ?> <strong>Category</strong><br /> <select name="cat" size="1"> <option value="0"<?php echo $selected[0]; ?>></option> <option value="1"<?php echo $selected[1]; ?>>test</option> </select> And here's the checkbox. <input type="checkbox" name="agree" value="agreement">I agree. Thanks. Link to comment https://forums.phpfreaks.com/topic/118326-retaining-checkbox-value-after-refresh/ Share on other sites More sharing options...
akitchin Posted August 5, 2008 Share Posted August 5, 2008 checkboxes are only sent with the form if selected. that means a simple isset() on the POST element will tell you whether or not you should load it in the checked state. in order to do that, simply specify the attribute checked="true": <input type="checkbox" name="agree" value="agreement"<?php if (isset($_POST['agreement'])) echo ' checked="checked"'; ?>>I agree. Link to comment https://forums.phpfreaks.com/topic/118326-retaining-checkbox-value-after-refresh/#findComment-608941 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.