perky416 Posted March 6, 2011 Share Posted March 6, 2011 Hi guys On the edit profile page on my website, im trying to get is so that when the user first opens the page, if the database field reads "1" the checkbox is checked. However if the user unchecks it and makes a mistake on the form which returns an error, the checkbox remains unchecked. Also if the user unchecks the text box, the 1 in the database is changed to a 0. And vise versa. The only way i have got it to work succesfully is by using the following: <?php $test = $_POST['test']; $submit = $_POST['submit']; if (!isset($test)) { $test="0"; } else { $test="1"; } if ($submit) { mysql_query("UPDATE profiles SET test='$test' WHERE username='user'"); echo "ok"; } ?> <html> <form action="test.php" method="POST"> <input type="checkbox" name="test" <?php if ($submit) { if ($test=="1") { echo "checked='checked'";}} else { if ($row['test'] == "1") {echo "checked='checked'";}} ?> /> <input type="submit" name="submit" /> </form> </html> My problem is, i have about 60 check boxes on the page, and i dont really want to write the code below for all of them: if (!isset($test)) { $test="0"; } else { $test="1"; } Does anybody know a better way to do this so i dont end up with 420 lines of extra code? Many thanks. Link to comment https://forums.phpfreaks.com/topic/229791-looking-for-a-better-way-to-handle-check-boxes/ Share on other sites More sharing options...
flolam Posted March 6, 2011 Share Posted March 6, 2011 if you change the checkboxes' names to checkbox[name1], checkbox[name2] etc. you will have a $_POST["checkbox"] array with only those names that where checked Link to comment https://forums.phpfreaks.com/topic/229791-looking-for-a-better-way-to-handle-check-boxes/#findComment-1183650 Share on other sites More sharing options...
perky416 Posted March 6, 2011 Author Share Posted March 6, 2011 Hi flolam, Please could you elaberate? Iv just being doing some reading on your suggestion however i cant seem to find a way to get it to write 0 to the database if the checkbox is not selected. Thanks Link to comment https://forums.phpfreaks.com/topic/229791-looking-for-a-better-way-to-handle-check-boxes/#findComment-1183653 Share on other sites More sharing options...
flolam Posted March 6, 2011 Share Posted March 6, 2011 Yes, unchecked checkboxes are never submitted. You could, however, make an array with all checkbox names, create the checkboxes out of that array and than compare the $_POST["boxes"] array with the one you have. What I mean is this: <form action="/" method="post"> <?php $checkboxnames = array("chkbx1", "chkbx2", "chkbx3", "chkbx4", "chkbx5"); //all 60 names $checkboxvalues = array(); if ($_SERVER['REQUEST_METHOD'] == "POST") { foreach ($checkboxnames as $chkbx) { if (in_array($chkbx, array_keys($_POST["boxes"]))) { $checkboxvalues[$chkbx] = 1; } else { $checkboxvalues[$chkbx] = 0; } } /* $checkboxvalues will look like this: Array ( [chkbx1] => 1 [chkbx2] => 0 [chkbx3] => 0 [chkbx4] => 0 [chkbx5] => 1 ) */ } foreach ($checkboxnames as $name) { $checked = ""; if (isset($_POST["boxes"][$name])) { $checked = ' checked="checked"'; } echo $name.': <input type="checkbox" name="boxes['.$name.']" value="1"'.$checked.' /><br />'; } ?> <input type="submit" value="submit" /> </form> Link to comment https://forums.phpfreaks.com/topic/229791-looking-for-a-better-way-to-handle-check-boxes/#findComment-1183670 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.