-3

I would like to get all checked checkboxes in a form, so if there were a list of checkboxes with the values 'a', 'b', 'c' and 'd' and only checkbox 'a' and 'c' were checked, my query would only select 'a' and 'c' from the desired table.

What would be the simplest way of accomplishing this prefferably in PHP?

EDIT: After retrieving which checkboxes were checked, i want to use those in my MySQLI Query.

So if for example the result is 'c','d','e' the query should look like this:

$query = "SELECT 'c', 'd', 'e' FROM table";

How would i translate the results to this?

I tried using the sample from a similar question: https://stackoverflow.com/a/4997271/5453484

Edit 2: More code:

<form name="filter"  style="float:left;" method="post">
            <table>
                <tr>
                    <td>
                        <label>Voornaam:</label>
                    </td>
                    <td>
                        <input type="checkbox" name="check_list[]" value="a"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Achternaam:</label>
                    </td>
                    <td>
                        <input type="checkbox" name="check_list[]" value="b"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Adres</label>
                    </td>
                    <td>
                        <input type="checkbox" name="check_list[]" value="c"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Plaats</label>
                    </td>
                    <td>
                        <input type="checkbox" name="check_list[]" value="d"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Postcode</label>
                    </td>
                    <td>
                        <input type="checkbox" name="check_list[]" value="e"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Zoeken</label>
                    </td>
                    <td>
                        <input type="text" name="Zoeken" value="f"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label style="margin-top:5px;"></label>
                    </td>
                    <td>
                        <input id="submitfilter" type="submit" style="margin-top:5px;" class="btn">Zoeken</input>
                    </td>
                </tr>
            </table>
            <?php
            if(!empty($_POST['check_list'])) {
                foreach($_POST['check_list'] as $check) {
                    echo $check; //echoes the value set in the HTML form for each checked checkbox.
                    //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
                    //in your case, it would echo whatever $row['Report ID'] is equivalent to.


                }
            }
            ?>
        </form>

This is the form i use at the moment.

Community
  • 1
  • 1
Wessah
  • 101
  • 13
  • Why not use jQuery to detect the value of the checked checkboxes and send it to your PHP script via AJAX/ POST? – Kieron606 Apr 07 '16 at 08:46
  • @Kieron606 Would also be an option, i'm still new to JQuery so i wouldn't know how to write a decent explanation myself. – Wessah Apr 07 '16 at 08:48
  • 2
    Simplest way of doing this is using [] after the name of the checkbox and offcourse giving the checkboxes the same name.. – Naruto Apr 07 '16 at 08:49
  • Possible duplicate of [Get $\_POST from multiple checkboxes](http://stackoverflow.com/questions/4997252/get-post-from-multiple-checkboxes) – Naruto Apr 07 '16 at 08:50
  • Unless you want your page to react/change to the choice of checkboxes instantly, there is no need to go through the overhead of script/AJAX. Just submit the page, check the values and return whatever you want to return. Adding your code to the question will make it easier to point you in the right direction. – RST Apr 07 '16 at 08:59
  • http://php.net/manual/en/language.variables.external.php#34820 – deceze Apr 07 '16 at 09:02
  • @RST i've edited the post, i left an example of what should be in the right direction but i don't know how to use that in a query yet. – Wessah Apr 07 '16 at 09:08
  • 1
    `'SELECT ' . join(', ', $checkboxen) . ' FROM table'`...!? Of course, you'll want to whitelist your values here to avoid SQL injection and containing the result to allowed columns. – deceze Apr 07 '16 at 09:11
  • You should share more code. Your HTML and the PHP you tried – RST Apr 07 '16 at 09:32
  • @RST just edited in the whole form & php i currently use for it. – Wessah Apr 07 '16 at 09:41
  • Almost there. What does your database look like? Without knowing the structure of `table` it is impossible to create the correct query. – RST Apr 07 '16 at 15:54

2 Answers2

0

Try add Jquery to your page. Assuming checkbox class as .checkboxtd.

$(document).on('change','.checkboxtd',function () {

if($(this).is(':checked'))
{
    //do some thing
}
else
{
    //do some thing
} 
});
GibboK
  • 71,848
  • 143
  • 435
  • 658
satish kilari
  • 746
  • 4
  • 21
0

I fixed it with the following code:

<form name="filter"  style="float:left;" method="post">
            <table>
                <tr>
                    <td>
                        <label>Voornaam:</label>
                    </td>
                    <td>
                        <input type="checkbox" name="first_name" id="inh" value="a_Voornaam"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Achternaam:</label>
                    </td>
                    <td>
                        <input type="checkbox" name="last_name" id="inh" value="a_Achternaam"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Adres</label>
                    </td>
                    <td>
                        <input type="checkbox" name="address" id="inh" value="a_Adres"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Plaats</label>
                    </td>
                    <td>
                        <input type="checkbox" name="city" id="inh" value="a_Woonplaats"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Postcode</label>
                    </td>
                    <td>
                        <input type="checkbox" name="zipcode" id="inh" value="a_Postcode"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Zoeken</label>
                    </td>
                    <td>
                        <input type="text" name="Zoeken" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <label style="margin-top:5px;"></label>
                    </td>
                    <td>
                        <input id="submitfilter" type="submit" style="margin-top:5px;" class="btn"/>
                    </td>
                </tr>
            </table>
            <?php
            if(!empty($_POST)){
                var_dump($_POST);

                foreach ($_POST as $key => $row){
                    if(empty($values)) {
                        $values = $row;
                    } else {
                        $values .= ', ' . $row;
                    }
                }
                $txt = $_POST['Zoeken'];
                echo $values;

                ?><br><?php
                echo $txt;
            }
            ?>
        </form>
Wessah
  • 101
  • 13