开发者

can't read the value of a checkbox array after submit a form

I don't know what I miss here but I try to make a really simple form with a checbox in it, here is the html:

(...)
    <input type="checkbox" name="test[]" value="js"> Javascript <br>
    <input type="checkbox" name="test[]" value="php">  PHP <br>
    <input type="checkbox" name="test[]" value="sql">  SQL <br>
    <input type="checkbox" name="test[]" value="html">  HTML <br>
(...)

and here is the php snippet which suppose to handle it:

    echo '<pre>';
    print_r($_POST['test']);
    echo '</pre>';
    print_r($_POST)
    echo '<hr>';

here is the output

Array
Array
(     
    [stuff1] => 0
    [stuff2] => 5
    [stuff3] => 2
    [test] => Array        
)

The other input of the forms are well display 开发者_运维技巧but I can't parse the content of the array which is just a sample string named "array"...

If i try to do a

var_dump($_POST["test"]); //this is what I get: string 'Array' (length=5)


There is a good tutorial about how to handle checkbox using php : http://www.html-form-guide.com/php-form/php-form-checkbox.html

Basically your $_POST['test'] is an array, that is empty if no checkbox was checked. And that would be like ["js","php"] if the user selected js and php.

If you want to cycle through all selected choices, you could do :

foreach ($it in $_POST['test']) {
   echo $it
}


I found it, the reason is that I'm using an old framework that make a strange statement within or without magic_quotes activated, Ive got magic_quotes activated so it's escaping any array contained in the $_POST variables...

foreach ($_POST as $key => $value) {
    if ($value && !$is_magic_quotes_gpc) {
        $_POST["$key"] = addslashes($value);
    }
}

I just remove this snippet and everything went allright

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜