krepepet Posted June 30, 2009 Share Posted June 30, 2009 hello. i am developing a system, and the system needs to handle so many checkboxes.. ??? ok. something like this.. form 1 : []a, []b, []c form 2 : []d, []e, []f, []g form 3 : []h, []i, []j, []k the form will be used to select data from database. i dont really have a good idea on how to handle the forms. i did this SELECT * FROM table WHERE (form1 = '$form[0]' OR form1 = '$form[1] OR form1 = '$form[2]') AND (form2 = '$form2[0]' OR form2 = '$form2[1]' ..) AND .. the thing is, not all box will be checked so it will cause error. if is_null is used, it will produced a very long if else statement. and i believe this is not a good technique to handle the checkboxes. is there any good article/example on the net on how to handle many checkboxes? i tried googling around but fail to find one that suits my project Link to comment https://forums.phpfreaks.com/topic/164217-form-processing-checkboxes/ Share on other sites More sharing options...
themarty Posted June 30, 2009 Share Posted June 30, 2009 I think this is more or less what you're looking for: <?php if (isset($_POST['check'])) { $query = "SELECT * FROM table WHERE field IN (".implode(",", array_keys($_POST['check'])).")"; echo $query; } ?> <form method="post"> 1: <input type="checkbox" name="check[1]"><br> 2: <input type="checkbox" name="check[2]" checked><br> 3: <input type="checkbox" name="check[3]" checked><br> 4: <input type="checkbox" name="check[4]"><br> <input type="submit"> </form> Link to comment https://forums.phpfreaks.com/topic/164217-form-processing-checkboxes/#findComment-866269 Share on other sites More sharing options...
krepepet Posted July 1, 2009 Author Share Posted July 1, 2009 I think this is more or less what you're looking for: <?php if (isset($_POST['check'])) { $query = "SELECT * FROM table WHERE field IN (".implode(",", array_keys($_POST['check'])).")"; echo $query; } ?> <form method="post"> 1: <input type="checkbox" name="check[1]"><br> 2: <input type="checkbox" name="check[2]" checked><br> 3: <input type="checkbox" name="check[3]" checked><br> 4: <input type="checkbox" name="check[4]"><br> <input type="submit"> </form> i succeeded in doing some if else with the arrays but for about 25 boxes, it will be too complicated. i believe theres a simpler way to do it. and this one looks like it. but a bit confused here, can u explain this part (".implode(",", array_keys($_POST['check'])).") Link to comment https://forums.phpfreaks.com/topic/164217-form-processing-checkboxes/#findComment-866889 Share on other sites More sharing options...
themarty Posted July 1, 2009 Share Posted July 1, 2009 but a bit confused here, can u explain this part (".implode(",", array_keys($_POST['check'])).") after posting the form, $_POST['check'] will be an array that consists of key-value pairs in which the keys are the numbers in, for example, name="check[3]" of all the checkboxes that were selected. (the value will be 'on' since we didn't specify one ourselves). array_keys creates an array of all these keys (so if you selected box 2 & 3 it will create an array like this: array(2,3) and finally implode creates a coma separated string of these values -> '2,3' So that gives us this query: SELECT * FROM table WHERE field IN (2,3) which selects all the 'field' fields that have either the value 2 or 3. It's the same as writing: SELECT * FROM table WHERE (field=2 OR field=3) Link to comment https://forums.phpfreaks.com/topic/164217-form-processing-checkboxes/#findComment-867017 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.