如何使用 PHP post 方法提交复选框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2309801/
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 05:59:10  来源:igfitidea点击:

How to submit checkbox values with PHP post method

php

提问by Egglabs

<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" />

The from has been submitted using the "POST" method. identify, which of the check-boxes and write their numbers in increasing order. Separated all numbers by spaces(not new lines) and do not use any HTML formatting.

来自已使用“POST”方法提交。确定哪些复选框并按升序写下它们的编号。用空格(不是换行)分隔所有数字,并且不使用任何 HTML 格式。

For Eg:

例如:

If check-boxes 3, 5 and 10 are checked.

如果复选框 3、5 和 10 被选中。

Ouput would be:

输出将是:

3 5 10

3 5 10

回答by vsr

Change the markup to something like

将标记更改为类似

<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[]" />

and to get the values submitted, use a simple loop

并获取提交的值,使用一个简单的循环

foreach($_POST['checkbox'] as $checkbox){
    echo $checkbox . ' ';
}

回答by user2013

HTML Code ::

HTML 代码 ::

  <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::

php代码::

 $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

now you get the whole value of 'checkbox' in one variable

现在您可以在一个变量中获得“复选框”的全部值

回答by cletus

Iterate over the $_POSTarray and use preg_match()to pull out the number if it starts with "checkbox_":

迭代$_POST数组并用于preg_match()提取以“checkbox_”开头的数字:

$checked = array();
foreach ($_POST as $k => $v) {
  if (preg_match('|^checkbox_(\d+)$!', $k, $matches) {
    $checked[] = $matches[1];
  }
}
echo implode(' ', $matches);

回答by Rima

<?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 .' ';
?>

回答by Sashi

For your Each Checkbox value retrieval you can use below method to get values from checkbox are checked or not....

对于您的每个复选框值检索,您可以使用以下方法从复选框中获取值是否被选中....

In My Form Page (Monday to Sunday)

在我的表单页面(周一至周日)

 <input type="checkbox" name="checked[]" class="onoffswitch-checkbox" id="mononoffswitch" value="Mon" checked>

In My PHP Code

在我的 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";

}

Now you can get these variable values and can use in to your Insert Into statement ...

现在您可以获得这些变量值并可以用于您的 Insert Into 语句......

Like this

像这样

$addweekdays = mysqli_query($conn, "INSERT INTO weekdays(id,monday,tuesday,wednesday,thursday,friday,saturday,sunday) VALUES('$Id', '$checkedMon', '$checkedTue', '$checkedWed', '$checkedThur','$checkedFri','$checkedSat','$checkedSun')") ...

回答by jeevesh kumar

$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 .' ';

回答by Shashank Mishra

take an array for name of your checkbox as name="checkbox[]"and use same name for all your checkbox. After submitting your form receive the values of checked checkbox by using following code:

取一个数组作为复选框的名称,name="checkbox[]"并为所有复选框使用相同的名称。提交表单后,使用以下代码接收选中复选框的值:

<?php
$chk="";
foreach($_POST['checkbox'] as $checkbox)
{
$chk=$chk.$checkbox;
}
echo $chk;
?>