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. Quote Link to comment 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. Quote Link to comment 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.