如何检查是否收到$_GET? (PHP)

发布于 2024-10-25 03:10:44 字数 851 浏览 0 评论 0原文

我在页面上有一个表格。它有太多的东西,比如复选框、文本字段和下拉菜单。 但是,当我不选择其中一个复选框时,我捕获表单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爺獨霸怡葒院 2024-11-01 03:10:44

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.

人间☆小暴躁 2024-11-01 03:10:44

复选框值仅在选中时才被注册。因此,您可以简单地执行如下检查:

$check1 = isset($_GET['check1']);
$check2 = isset($_GET['check2']);

$check1$check2 现在将具有布尔值:

echo "Checkbox 1 one is " . ($check1 ? "checked" : "not checked") . PHP_EOL;
echo "Checkbox 2 one is " . ($check2 ? "checked" : "not checked") . PHP_EOL;

The checkbox value is only registered, when it's checked. So you can simply do a check like this:

$check1 = isset($_GET['check1']);
$check2 = isset($_GET['check2']);

$check1 and $check2 will now have boolean values:

echo "Checkbox 1 one is " . ($check1 ? "checked" : "not checked") . PHP_EOL;
echo "Checkbox 2 one is " . ($check2 ? "checked" : "not checked") . PHP_EOL;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文