<input type="checkbox" class='form' name="checkbox_1" />
<input type="checkbox" class='form' name="checkbox_2" />
<input type="checkbox" class='form' name="checkbox_3" />
.........
<input type="checkbox" class='form' name="checkbox_10" />
已使用“POST”方法提交了from。确定哪个复选框并按递增顺序写出它们的数字。用空格(而不是新行)分隔所有数字,不要使用任何HTML格式。
例如:
如果选中复选框3,5和10。
输出将是:
3 5 10
答案 0 :(得分:21)
将标记更改为
<input type="checkbox" class='form' value="1" name="checkbox[]" />
<input type="checkbox" class='form' value="2" name="checkbox[]" />
<input type="checkbox" class='form' value="3" name="checkbox[]" />
要获取提交的值,请使用简单的循环
foreach($_POST['checkbox'] as $checkbox){
echo $checkbox . ' ';
}
答案 1 :(得分:12)
HTML Code ::
<input type="checkbox" name="arrayValue[]" value="value1" > value1 <br />
<input type="checkbox" name="arrayValue[]" value="value2" > value2 <br />
<input type="checkbox" name="arrayValue[]" value="value3" > value3 <br />
<input type="checkbox" name="arrayValue[]" value="value4" > value4 <br />
php code ::
$checkBoxValue = join(", ", $_POST['arrayValue']); // here first (,) is user-define
// means, you can change it whatever
// you want, even if it can be (space) or others
现在你可以在一个变量
中获得'checkbox'的全部值答案 2 :(得分:5)
对$_POST
数组进行迭代,并使用preg_match()
提取数字,如果它以“checkbox _”开头:
$checked = array();
foreach ($_POST as $k => $v) {
if (preg_match('|^checkbox_(\d+)$!', $k, $matches) {
$checked[] = $matches[1];
}
}
echo implode(' ', $matches);
答案 3 :(得分:4)
<?php
$checked = array();
foreach ($_POST as $k => $v) {
$subject = $k;
$pattern = '/^checkbox_(\d+)$/';
if (preg_match($pattern, $subject, $matches))
{
$checked[] = $matches[1];
}
}
asort($checked);
foreach ($checked as $key=>$value)
echo $value .' ';
?>
答案 4 :(得分:0)
对于每个Checkbox值检索,您可以使用以下方法从复选框中获取值是否已选中....
在我的表格页面(周一至周日)
<input type="checkbox" name="checked[]" class="onoffswitch-checkbox" id="mononoffswitch" value="Mon" checked>
在我的PHP代码中
function IsChecked($chkname,$value)
{
if(!empty($_POST[$chkname]))
{
foreach($_POST[$chkname] as $chkval)
{
if($chkval == $value)
{
return true;
}
}
}
return false;
}
if(IsChecked('checked','Mon'))
{
$checkedMon = "Y";
}else {
$checkedMon = "N";
}
if(IsChecked('checked','Tue'))
{
$checkedTue = "Y";
}else {
$checkedTue = "N";
}
if(IsChecked('checked','Wed'))
{
$checkedWed = "Y";
}else {
$checkedWed = "N";
}
if(IsChecked('checked','Thur'))
{
$checkedThur = "Y";
}else {
$checkedThur = "N";
}
if(IsChecked('checked','Fri'))
{
$checkedFri = "Y";
}else {
$checkedFri = "N";
}
if(IsChecked('checked','Sat'))
{
$checkedSat = "Y";
}else {
$checkedSat = "N";
}
if(IsChecked('checked','Sun'))
{
$checkedSun = "Y";
}else {
$checkedSun = "N";
}
现在您可以获取这些变量值,并可以使用Insert Into语句...
喜欢这个
$addweekdays = mysqli_query($conn, "INSERT INTO weekdays(id,monday,tuesday,wednesday,thursday,friday,saturday,sunday) VALUES('$Id', '$checkedMon', '$checkedTue', '$checkedWed', '$checkedThur','$checkedFri','$checkedSat','$checkedSun')") ...
答案 5 :(得分:0)
$checked = array();
foreach ($_POST as $k => $v) {
$subject = $k;
$pattern = '/^checkbox_(\d+)$/';
if (preg_match($pattern, $subject, $matches))
{
$checked[] = $matches[1];
}
}
asort($checked);
foreach ($checked as $key=>$value)
echo $value .' ';
答案 6 :(得分:0)
将一个数组作为复选框的名称name="checkbox[]"
,并为所有复选框使用相同的名称。
提交表单后,使用以下代码接收已选中复选框的值:
<?php
$chk="";
foreach($_POST['checkbox'] as $checkbox)
{
$chk=$chk.$checkbox;
}
echo $chk;
?>