如何在点击按钮时将值从复选框获取到php数组



我有个问题。我想从复选框中获得选中的值,并将它们添加到php数组中,但我不知道如何做到这一点。我可以用javascript来做,但我想要一个php数组,我不知道怎样将js数组转换为php数组。我将在下一个php文件中使用数组,并使用这些值从数据库中搜索行。如果有人建议我怎么做,我将不胜感激,因为我不知道。

while ($row = mysqli_fetch_array($search)) {
$category = $row["Category"];
echo<inputtype="checkbox"name="category"value="$category"required/>$kategoria</input><br /><br />";
}
<a id='answer' href='questions.php'><button>Answer to questions!</button></a>

这就是我如何制作复选框,然后点击按钮,我移动到下一个文件

add attribute action=questions.php,method="张贴";以形成而不是使用<a href="questions.php">并将其放入表单中,并在问题的第一行使用extract($$__REQUEST(。php使用print_r(extract(($_REQUEST((将表单中提取的数据打印到关联数组中。

将其用于表单:

<form method="POST" action="questions.php">
<input type="checkbox" name="myInput" id="myInput" value="checked" />
<label for="myInput">Checkbox</label>
<!-- A submit button -->
<input type="submit" value="Submit values" />
</form>

然后在questions.php中,所有值都被添加到变量$_POST:中

// Foreach item in the POST array.
foreach ($_POST as $item => $value) {
// The value will be stored in $value
}

如果选中该复选框,$value变量将被选中。否则它将是空的。

来源:PHP:多维数组中的Foreach

最新更新