pzoli89
pzoli89

Reputation: 52

Checkbox Value If name is dynamic

I have a list of checkbox generated from database:

foreach($array as $value){

echo '<input type="checkbox" name="t' . $value . '" value="0"/>';

}

Question:

How can I get the values of these checkboxes?

I tried in this way:

foreach ($array as $value) {
            $perm = $_REQUEST["t$value"];
}

But is not working. :(

Upvotes: 0

Views: 62

Answers (4)

dododo
dododo

Reputation: 266

To above written: You also need check isset()

foreach($array as $key=>$value){
    if(isset($_REQUEST["t".$value]))
        $perm[$key] = $_REQUEST["t".$value];
}
var_dump($perm);

Or you can throws Warning exception if variable (array key) is not set. Because browser can send only checked values and skip unchecked in request.

Upvotes: 1

TiMESPLiNTER
TiMESPLiNTER

Reputation: 5899

I would suggest the following

foreach($array as $value){

    echo '<input type="checkbox" name="t[]" value="' . $value . '"/>';

}

Then you can access all the choosed options in PHP as array with

$_REQUEST['t']

Upvotes: 2

&#200;cid&#233;
&#200;cid&#233;

Reputation: 66

You want to create a associative array?

foreach($array as $value){

echo '<input type="checkbox" name="t['.$value.']" value="0"/>';

}

Then...

foreach ($array as $value) {
     $perm = $_REQUEST["t"][$value];
}

Upvotes: 0

Jenz
Jenz

Reputation: 8369

Try this

foreach ($array as $value) {
            $perm = $_REQUEST["t".$value];
}

Upvotes: 0

Related Questions