如何检查是否收到$_GET? (PHP)
我在页面上有一个表格。它有太多的东西,比如复选框、文本字段和下拉菜单。 但是,当我不选择其中一个复选框时,我捕获表单的 PHP 页面会显示错误。
示例:
HTML 代码:
Checkbox 1<input type="checkbox" name="check1" value="on" />
Checkbox 2<input type="checkbox" name="check2" value="on" />
PHP 代码:
$check1 = $_GET['check1'];
$check2 = $_GET['check2'];
如果两个项目都被选中并在 URL 中发送,则效果很好:
localhost/project/checkbox.php?check1=on&check2=on
但是当我取消选择其中 1 个项目时,假设 check2,则 URL 如下所示:
localhost/project/checkbox.php?check1=on
它向我显示一个错误 - $check2 是一个未定义的索引。
但如果未选中该复选框,我不希望它显示错误。我还尝试了 if
语句来检查我是否在 URL 中获取了它,但它不起作用。
有没有办法首先检查 URL 中是否传递了数据?因为我没有收到错误。 实际上,错误并不是主要的事情,因为我得到了正确的结果,并且我知道我可以关闭 php.ini 中的错误报告,但这不是我想要做的。我希望它首先检查数据是否传入?
谢谢...
I have a form on the page. It has too many things like checkboxes and text fields and dropdowns.
But when I don't select one of the checkboxes, the PHP page where i catch the form shows me an error.
Example:
The HTML Code:
Checkbox 1<input type="checkbox" name="check1" value="on" />
Checkbox 2<input type="checkbox" name="check2" value="on" />
The PHP Code:
$check1 = $_GET['check1'];
$check2 = $_GET['check2'];
It works fine if both the items are selected and sent in the URL:
localhost/project/checkbox.php?check1=on&check2=on
But when i deselect 1 of them, suppose check2, then the URL is like this:
localhost/project/checkbox.php?check1=on
and it shows me an error - that $check2 is an undefined index.
But I don't want it to show the error if the checkbox is not being selected. I also tried an if
statement to check if i'm getting it in the URL but it didn't work.
Is there a way to first check weather the data is being passed in the URL or not? As I don't get the error.
Actually the error is not the main thing, as I'm getting right results and I know I can switch off error reporting in php.ini, but thats not what I want to do. I want it to first check if data is coming in?
Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
if (isset($_GET['check2']) && !empty($_GET['check2'])
将检查 check2 GET 参数是否存在且不为空。if (isset($_GET['check2']) && !empty($_GET['check2'])
will do the check if check2 GET parameter is present and is not empty.复选框值仅在选中时才被注册。因此,您可以简单地执行如下检查:
$check1
和$check2
现在将具有布尔值:The checkbox value is only registered, when it's checked. So you can simply do a check like this:
$check1
and$check2
will now have boolean values: