Jump to content

form processing - checkboxes


krepepet

Recommended Posts

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

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 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'])).")

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.