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

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

问题描述

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

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.

我的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>

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

I pass the values to php by the $_GET method

我的php代码:

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


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

输出是真的错误的假

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

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

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

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'])) { ... }

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

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.

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

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

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

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.

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

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.

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

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天全站免登陆