php 从复选框中获取所有值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5182938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:34:22  来源:igfitidea点击:

Get all values from checkboxes?

phphtmlmysqlcheckbox

提问by Jay Wit

Is there an easy way to get the values of multiple checkboxes and store them in the database?

有没有一种简单的方法来获取多个复选框的值并将它们存储在数据库中?

<?php
if(isset($_POST['go'])){
   $fruit = $_POST['fruit'].",";
   echo $fruit;
   // if you selected apple and grapefruit it would display apple,grapefruit
}
?>
<form method="post">
Select your favorite fruit:<br />
<input type="checkbox" name="fruit" value="apple" id="apple" /><label for="apple">Apple</label><br />
<input type="checkbox" name="fruit" value="pinapple" id="pinapple" /><label for="pinapple">Pinapple</label><br />
<input type="checkbox" name="fruit" value="grapefruit" id="grapefruit" /><label for="grapefruit">Grapefruit</label><br />
<input type="submit" name="go" />
</form>

回答by Jeff Parker

If you give the checkboxes the same name, ending in [], the values are returned as an array.

如果您为复选框指定相同的名称,并以 [] 结尾,则这些值将作为数组返回。

<input type="checkbox" name="fruit[]" value="apple" />
<input type="checkbox" name="fruit[]" value="grapefruit" />

Then in PHP ...

然后在 PHP ...

if( isset($_POST['fruit']) && is_array($_POST['fruit']) ) {
    foreach($_POST['fruit'] as $fruit) {
        // eg. "I have a grapefruit!"
        echo "I have a {$fruit}!";
        // -- insert into database call might go here
    }

    // eg. "apple, grapefruit"
    $fruitList = implode(', ', $_POST['fruit']);
    // -- insert into database call (for fruitList) might go here.
}

PS. please forgive the obvious error, that this example will potentially shout "I have a apple" ... I didn't think to make the example smart enough to determine when to use "a", and when to use "an" :P

附注。请原谅明显的错误,这个例子可能会大喊“我有一个苹果”......我不认为让这个例子足够聪明来确定何时使用“a”,何时使用“an”:P

回答by grunk

Name your input like this :

像这样命名您的输入:

<input type="checkbox" name="fruit[]" value="apple" id="apple" /><label for="apple">Apple</label><br />
<input type="checkbox" name="fruit[]" value="pinapple" id="pinapple" /><label for="pinapple">Pinapple</label><br />

Then iterate on the $_POST['fruit'] :

然后迭代 $_POST['fruit'] :

if(isset($_POST['fruit']) && !empty($_POST['fruit']))   
    foreach($_POST['fruit'] as $fruit) echo $fruit;

回答by Jessica Brown

To add on to the other solutions...

要添加到其他解决方案...

implode and explode can be used in PHP to convert your array of fruit[] into a comma seperated list.

内爆和爆炸可以在 PHP 中使用将您的fruit[] 数组转换为逗号分隔的列表。

$valueToStoreInDB = implode(",",$_POST['fruit']);
$fruitAsAnArrayAgain[] = explode(",",$dbvalue);

Once you have your comma separated list, a good data type to represent the checkboxes in MySQL would be SET, and you can paste your comma seperated list right into your insert statement.

一旦你有了逗号分隔的列表,一个很好的数据类型来表示 MySQL 中的复选框将是SET,你可以将逗号分隔的列表直接粘贴到插入语句中。