确定复选框是否被选中 php $_GET

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

determine whether checkbox is checked php $_GET

phpget

提问by jamex

I just want to have php determines whether a checkbox is checked, but I am running into a problem of getting the right return. Help please.

我只想让 php 确定是否选中了一个复选框,但我遇到了获得正确回报的问题。请帮忙。

My html code

我的html代码

    <label>
    <input name="Language" type="checkbox" id="aa"  checked="checked"  />
    One</label>
    <label>
    <input name="Language" type="checkbox" id="bb" />Two</label>
    <label>
   <input type="checkbox" name="Language"  id="cc" />Three</label>

I pass the values to php by the $_GET method

我通过 $_GET 方法将值传递给 php

my php code:

我的 php 代码:

$aa=$_GET["aa"];
$bb=$_GET["bb"];
$cc=$_GET["cc"];


echo $aa."<br>";
echo $bb."<br>";
echo $cc."<br>";

the output is true false false

输出为真假假

I next want to just determine if each box is checked and if so, do something.

接下来我想确定是否每个框都被选中,如果是,做一些事情。

if ($aa == true) { $abc="checked";}
else { $abc="not checked"; }

if ($bb == true) { $cde="checked";}
else { $cde="not checked"; }


if ($fgh == true) { $fgh="checked";}
else { $fgh="not checked"; }

But the if statements always return true, even if the box is not checked. I tried variations of "===" and "!=", but it does not seem to work.

但是 if 语句始终返回 true,即使未选中该框。我尝试了“===”和“!=”的变体,但似乎不起作用。

TIA

TIA

回答by Quentin

if (isset($_GET['checkbox_name'])) { ... }

Form controls (with the exception of file inputs, and with some special rules for image inputs) always submit strings. There is no concept of a boolean in a query string or a form encoded POST body.

表单控件(除了文件输入,还有一些特殊的图像输入规则)总是提交字符串。查询字符串或表单编码的 POST 正文中没有布尔值的概念。

The id is irrelevant — only the name and value matter (at least as far as PHP is concerned).

id 无关紧要——只有名称和值很重要(至少就 PHP 而言)。

Since you haven't given them values they will, IIRC, default to onso if you are doing a comparison you should look for that. Looking with issetis simpler though. This is somewhat beside the point though, since your example gives them all the same name and value, so you can't differentiate between them.

因为你没有给他们赋值,on所以IIRC 默认是这样,如果你在做比较,你应该寻找那个。isset不过,使用更简单。不过,这有点离题,因为您的示例为它们提供了相同的名称和值,因此您无法区分它们。

Additionally, due to an oddity of the PHP form data parser, you have to end the with []if you want multiple elements with the same name.

此外,由于 PHP 表单数据解析器的奇怪之处,[]如果您想要多个具有相同名称的元素,则必须以 with 结尾。

You probably want to do something like this:

你可能想做这样的事情:

<label>
<input name="Language[]" type="checkbox" id="aa"  checked="checked" value="One" />
One</label>
<label>
<input name="Language[]" type="checkbox" id="bb" value="Two" />Two</label>
<label>
<input type="checkbox" name="Language[]"  id="cc" value="Three" />Three</label>

Important: Note the addition of values and the change in name.

重要提示:注意值的添加和名称的更改。

Then in PHP $_GET['Language']will be an Array of the values of the checked checkboxes, which you can loop over.

然后在 PHP$_GET['Language']中将是一个选中复选框的值的数组,您可以循环遍历。

回答by Prasoon Saurav

Try isset()

尝试 isset()

I think your HTML code should be like

我认为你的 HTML 代码应该像

<label>
  <input name="Language[]" type="checkbox" id="aa" checked="checked" value ="1" />One    
</label>
<label>
  <input name="Language[]" type="checkbox" id="bb" value ="2" />Two
</label>
<label>
  <input name="Language[]" type="checkbox"  id="cc" value ="3" />Three
</label>

and then by using something like

然后通过使用类似的东西

$count = count($_GET["Language"]);you can count the number of checkboxes checked.

$count = count($_GET["Language"]);您可以计算选中的复选框的数量。

Or do

或者做

$arr = $_GET["Language"]; //$arr is an array that contains the values of the checked checkboxes

Then you can foreachover the array

然后你可以foreach在数组上

   foreach( $arr as $item)
   {
        echo $item . "</br>"; /* Will print 1,2 and 3 (mind newlines)*/
   }

回答by Brian Hooper

In my PHP I got it from $_POST['checkbox_name'], and I found that the variable had the value onwhen the box was checked (i.e. it existed even if the checkbox was clear).

在我的PHP我是从$_POST['checkbox_name'],我发现有变量的值on时被选中此复选框(即它的存在,即使该复选框很清楚)。