$_REQUEST 中的复选框数组值,但 $_POST 中不存在

发布于 2024-09-14 01:42:31 字数 1067 浏览 1 评论 0原文

由于某种原因,我的复选框数组值没有显示在 $_POST 中。

例如:

<form method="post" action="">
    <input type="checkbox" name="cb[]" value="1">
    <input type="checkbox" name="cb[]" checked="checked" value="2">
    <input type="checkbox" name="cb[]" value="3">
    <input type="checkbox" name="cb[]" checked="checked" value="4">
    <input type="checkbox" name="cb[]" checked="checked" value="5">
    <input type="checkbox" name="cb[]" value="6">
    ...
    <input type="checkbox" name="cb[]" checked="checked" value="26">
    <input type="checkbox" name="cb[]" value="27">
    <input type="submit" value="insanitizer"/>
</form>

提交时:

<?php
print_r($_POST); //Because print_r($_POST['cb']); gives ''

Array (
   [category] =>
)

print_r($_REQUEST['cb']);  //Showing the correct array name was used

Array
(
    [0] => 2
    [1] => 4
    [2] => 5
    [3] => 26
)
?>

我很高兴至少可以在这里获取复选框数据,但我留下一个问题:

Wtf?

For some reason my checkbox array values don't show up in $_POST.

For example:

<form method="post" action="">
    <input type="checkbox" name="cb[]" value="1">
    <input type="checkbox" name="cb[]" checked="checked" value="2">
    <input type="checkbox" name="cb[]" value="3">
    <input type="checkbox" name="cb[]" checked="checked" value="4">
    <input type="checkbox" name="cb[]" checked="checked" value="5">
    <input type="checkbox" name="cb[]" value="6">
    ...
    <input type="checkbox" name="cb[]" checked="checked" value="26">
    <input type="checkbox" name="cb[]" value="27">
    <input type="submit" value="insanitizer"/>
</form>

When submit:

<?php
print_r($_POST); //Because print_r($_POST['cb']); gives ''

Array (
   [category] =>
)

print_r($_REQUEST['cb']);  //Showing the correct array name was used

Array
(
    [0] => 2
    [1] => 4
    [2] => 5
    [3] => 26
)
?>

I'm happy that I can at least get the checkbox data here, but I'm left with one question:

Wtf?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

魂牵梦绕锁你心扉 2024-09-21 01:42:31

Dur dur du...

作为一般初始化的一部分,我运行 $_POST 和 $_GET 通过:

<?php
if(sizeof($_POST) > 0){
    foreach($_POST as $key => $value){
        $_POST[$key] = $this->_db->realEscapeString($value);
    }
}
if(sizeof($_GET) > 0){
    foreach($_GET as $key => $value){
        $_GET[$key] = $this->_db->realEscapeString($value);
    }
} 
?>

这似乎会破坏任何数组...

上面替换为:

<?php
...
if(sizeof($_GET) > 0){
        $this->initDbCleanArray($_GET);
    }
}
...

private function initDbCleanArray($a)
{
    if(sizeof($a) > 0){
        foreach($a as $key => $value){
            if(is_array($a[$key])){
                $this->initDbCleanArray($a[$key]);
            }
            else{
                $a[$key] = $this->_db->realEscapeString($value);
            }
        }
    }
}
?>

realEscapeString = mysql_real_escape_string

...和 ​​$_POST['cb'] 活着!

Dur dur dur...

As part of general initialization I run $_POST and $_GET through:

<?php
if(sizeof($_POST) > 0){
    foreach($_POST as $key => $value){
        $_POST[$key] = $this->_db->realEscapeString($value);
    }
}
if(sizeof($_GET) > 0){
    foreach($_GET as $key => $value){
        $_GET[$key] = $this->_db->realEscapeString($value);
    }
} 
?>

Which seems to nuke any arrays...

Replaced above with:

<?php
...
if(sizeof($_GET) > 0){
        $this->initDbCleanArray($_GET);
    }
}
...

private function initDbCleanArray($a)
{
    if(sizeof($a) > 0){
        foreach($a as $key => $value){
            if(is_array($a[$key])){
                $this->initDbCleanArray($a[$key]);
            }
            else{
                $a[$key] = $this->_db->realEscapeString($value);
            }
        }
    }
}
?>

realEscapeString = mysql_real_escape_string

...and $_POST['cb'] lives!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文