简体   繁体   English

从我的数据库中的复选框保存值

[英]Saving values from checkboxes in my db

I am trying to store the values from checkboxes inside my database. 我正在尝试将复选框中的值存储在数据库中。 So far, I was only able to store the last checkbox choice. 到目前为止,我只能存储最后一个复选框选择。 Afterwards, I tried following someone's example, but I got so confused. 之后,我尝试遵循某人的榜样,但感到非常困惑。 How could I ensure that all my data is being stored inside the database? 如何确保所有数据都存储在数据库中?

In my table, my column which I want to contain the information is named "categories". 在我的表中,我要包含信息的列称为“类别”。 As for the rest, it is below (2 of them only) 至于其余的,则低于(仅2个)

<tr>
    <td><input type="hidden" name="categories" value="categories">Categories</td>       

        <td>
            <div class="checkbox";>
                <label class="checkbox_label">
                    <input name="cats" value="Accessoires" type="checkbox" class="checkbox1"/>
                    Accessoires
                </label>
            </div>

            <div class="checkbox";>
                <label class="checkbox_label">
                    <input name="cats" value="Mobilier de bureau " type="checkbox" class="checkbox1"/>
                    Mobilier de bureau 
                </label>
            </div>
    </td>
</tr>

This is the code I tried following: 这是我尝试过的代码:

$box=$_POST['box'];
$ledlamps = $_POST['ledlamps'];

$str = $ledlamps . ": " . implode(", ", $box);

And so I ended up with this: 所以我最终得到了这个:

$cats = $_POST['cats'];
$categories=$_POST['categories'];

$str = $categories . ": " . implode(", ", $cats);

I do not have sufficient privileges to add a comment to the previous answer attempt but what @Erman Belegu was saying is right, cats[] should do the trick. 我没有足够的特权向尝试的答案添加评论,但是@Erman Belegu所说的正确, cats[]应该可以解决问题。 Here is the code. 这是代码。

The HTML HTML

<tr>
    <td><input type="hidden" name="categories" value="categories">Categories</td>       

        <td>
            <div class="checkbox";>
                <label class="checkbox_label">
                    <input name="cats[]" value="Accessoires" type="checkbox" 
                      class="checkbox1"/>
                    Accessoires
                </label>
            </div>

            <div class="checkbox";>
                <label class="checkbox_label">
                    <input name="cats[]" value="Mobilier de bureau " 
                        type="checkbox" class="checkbox1"/>
                    Mobilier de bureau 
                </label>
            </div>
    </td>
</tr>

<input type="submit" name="submit" value="submit"/>

The only thing I added was the submit button so I could only do something in the php when submit is clicked, here is the same php code. 我唯一添加的是提交按钮,因此单击提交时我只能在php中做某事,这是相同的php代码。

<?php

$cats = array();

    if(isset($_POST['submit']))
    {
        if(isset($_POST['cats']))
        {
            $cats = $_POST['cats'];
        }
        $categories= $_POST['categories'];

        $str = $categories . ": " . implode(", ", $cats);
        echo $str;
    }

?>

If you select something and click echo you will see categories: and whatever you selected. 如果选择某项并单击echo,则将看到类别:以及您选择的任何内容。

Hope this helps or perhaps I did not get exactly what your problem was in the first place. 希望这会有所帮助,或者也许我一开始就没有确切地了解您的问题。

Thanks. 谢谢。

The name of input shoud be name="cats[]" . 输入的名称应为name="cats[]" In these form you will get array of value that you checked. 通过这些形式,您将获得检查的值数组。

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

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