Hi, I created a form with some checkboxes. When a user checks the box and submits, the form is redisplayed with the results but the check is removed. I have been able to make the text entries stick but not the checkbox after submitting. How would I do this? thanks.
Hey. It's quite simple but involves a little logic: <?php // Coded by Daniel Clarke (DanMakes) function checkbox($name,$value) { // Get the value of the form and then if it's been checked. if ($_GET[$name] == $value) { // if it has return the HTMl for a checked checkbox. return 'checked="checked"'; } } ?> <input name="food" type="checkbox" <?php checkbox("food","ham") ?> id="checkbox" value="ham" /> PHP: So use that function and then inside of the checkbox code type <?php checkbox("CHECKBOXNAME","CHECKEDVALUE") ?>. Hope that makes sense
I use this function: function checkbox($var){ if($var) return 'checked="checked"'; } Code (markup): then i call the funcion on the checkbox: <input type="checkbox" name="boxName" value="1" <?=checkbox($_POST['boxName'])?> /> Code (markup): or directly without a function: <input type="checkbox" name="boxName" value="1" <?=(($_POST['boxName']=='1')?'checked="checked"':'')?> /> Code (markup):