简体   繁体   English

获取 PHP 中哪些复选框已被选中

[英]Get which checkboxes have been checked in PHP

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.我想在表单中获取所有选中的复选框,所以如果有一个复选框列表,其中包含值为“a”、“b”、“c”和“d”,并且只选中了复选框“a”和“c” ,我的查询只会从所需的表中选择“a”和“c”。

What would be the simplest way of accomplishing this prefferably in PHP?在 PHP 中最好地完成此操作的最简单方法是什么?

EDIT: After retrieving which checkboxes were checked, i want to use those in my MySQLI Query.编辑:检索哪些复选框被选中后,我想在我的 MySQLI 查询中使用那些。

So if for example the result is 'c','d','e' the query should look like this:因此,如果例如结果是 'c','d','e' 查询应如下所示:

$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我尝试使用来自类似问题的示例: https : //stackoverflow.com/a/4997271/5453484

Edit 2: More code:编辑2:更多代码:

<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.这是我目前使用的表格。

Try add Jquery to your page.尝试将 Jquery 添加到您的页面。 Assuming checkbox class as .checkboxtd .假设复选框类为.checkboxtd

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

if($(this).is(':checked'))
{
    //do some thing
}
else
{
    //do some thing
} 
});

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM