确定复选框是否被选中php $ _GET [英] determine whether checkbox is checked php $_GET

查看:78
本文介绍了确定复选框是否被选中php $ _GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的html代码

 < label> 
< input name =Languagetype =checkboxid =aachecked =checked/>
One< / label>
< label>
< input name =Languagetype =checkboxid =bb/> Two< / label>
< label>
< input type =checkboxname =Languageid =cc/> Three< / label>

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



我的php代码:

  $ aa = $ _ GET [aa]; 
$ bb = $ _ GET [bb];
$ cc = $ _ GET [cc];


echo $ aa。< br>;
echo $ bb。< br>;
echo $ cc。< br>;

输出为
true
false
false



接下来我想确定每个盒子是否被选中,如果是,请执行一些操作。

  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; }

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



TIA

=h2_lin>解决方案

  c> 

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



id是无关紧要的 - 只有名称和值才重要(至少PHP是关注的)。



因为你没有给它们赋值,所以IIRC默认为 on so如果你正在做比较,你应该寻找那个。用 isset 看起来很简单。这有点旁边的,因为你的例子给他们所有相同的名称和价值,所以你不能区分它们。



另外,由于一个奇怪的的PHP表单数据解析器,如果您想要多个具有相同名称的元素,您必须使用 [] 结束。



您可能想要做这样的事情:

 < label> 
< input name =Language []type =checkboxid =aachecked =checkedvalue =One/>
One< / label>
< label>
< input name =Language []type =checkboxid =bbvalue =Two/> Two< / label>
< label>
< input type =checkboxname =Language []id =ccvalue =Three/> Three< / label>

重要:请注意添加值和名称更改



然后在PHP中 $ _ GET ['Language'] 将是选中复选框值的数组,你可以循环。


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.

My html code

    <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

my php code:

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

TIA

解决方案

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.

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

Since you haven't given them values they will, IIRC, default to on so if you are doing a comparison you should look for that. Looking with isset is 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.

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.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆