Working with a form that generates checkbox options from a database. I want to set a default option as checked (ticked). Does anyone know if i can do it or how to do it.
when you are writing the checkbox - create a variable before it that is equal to "" then do an if statement to see if it should be checked. If it is - then set your variable = " checked " then when you write your (x)html just enter that variable which is either equal to "" or equal to " checked "
// DB pull $query = mysql_query("select value FROM ..."); $checked; if ($row = mysql_fetch_assoc($query)) { if (isset($row['value'])) { $checked = "checked"; } } <input type="checkbox" name="value" id="value" {$checked}> PHP:
Use the following, obviously change to suit: <input type="checkbox" name="chkName" id="chkName" <?= ( isset($row[0]) ? ' checked="checked"' : '') ?> /> PHP: or if you need the form to remember the values of a checkbox (or any other field) <input type="checkbox" name="chkName" id="chkName" <?= ( isset($_REQUEST['chkName']) ? ' checked="checked"' : '') ?> /> PHP: