简体   繁体   English

如何将清单值保存到MySQL中并能够重新填充复选框?

[英]How to save checklist values into MySQL & be able to re-populate the checkboxes?

I cannot figure out how to save checked checkbox form input into MySQL and be able to retrieve them and re-populate the inputs. 我无法弄清楚如何将选中的复选框表单输入保存到MySQL中,并能够检索它们并重新填充输入。

I haven't even created a table yet because I cannot mentalize how go by doing it. 我什至还没有创建表,因为我无法弄清楚这样做的方式。

I have an html table with multiple rows(tr) and inside it all the tables have these checkboxes and I am wanting to save each of the selected boxes to MySQL and be able to retrieve the selected ones and re-populate it, but as I said I cannot figure out how to do it. 我有一个具有多行(tr)的html表,并且其中所有表都有这些复选框,我想将每个选定的框保存到MySQL,并能够检索选定的框并重新填充它,但是正如我说我不知道​​怎么做。

The only way I can think of is, submit the selected with a foreach and save all the selected ones, in each row with their specific table row iD but I don't understand how I would retrieve it if they are selected. 我能想到的唯一方法是,用foreach提交所选内容,并将所有所选内容保存在具有特定表行iD的每一行中,但是我不明白如果选择了它们,我将如何检索它。

I've got the following but no idea how it would be retrievable. 我有以下内容,但不知道如何获取。 Also, the checkboxes can increase at any time(the options) so I cannot create a specific table for them as there are currently 4 checkboxes, but tomorrow it can be 20 checkboxes to choose from. 另外,复选框可以随时增加(选项),因此我无法为它们创建特定的表,因为当前有4个复选框,但是明天可以选择20个复选框。

$checkboxData = $_POST['cbO'];
$tableiD      = '1';

if(isset($_POST['submit']))
{
    for( $i=0; $i < sizeof($checkboxData); $i++ )
    {
        $sth = $this->db->prepare("INSERT INTO table(rows) VALUES (:checkboxInfo) WHERE table_id = :tableiD");
        $sth->execute(array(
               ':checkboxInfo' => $checkboxData[$i],
               ':tableiD' => $tableiD)
        );
    }
}

<form method="post" action="">
    <li><input type="checkbox" name="cbO[]" value="349169846048613138">CheckBox1</li>
    <li><input type="checkbox" name="cbO[]" value="349169846048613136">CheckBox2</li>
    <li><input type="checkbox" name="cbO[]" value="349169846048613140">CheckBox3</li>
    <li><input type="checkbox" name="cbO[]" value="349169846048613141">CheckBox4</li>

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

Regarding the variable amount of items, get the items from the database and make a loop with them: 关于可变数量的项目,请从数据库中获取项目并对其进行循环:

<form method="post" action="">
<ul>
<?php if (!empty($items)) foreach ($itens as $i => $value) { ?>
    <li>
        <input type="checkbox" name="cbO[<?= $i ?>]" value="<?= $value ?>"> 
        CheckBox<?= ($i + 1) ?>
    </li>
<?php } ?>
</ul>
</form>

在期望接收的值类上运行print_r($_POST) ,然后该值将是您期望的值。

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

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