Reputation: 2413
I have a form that gets its names for checkboxes dynamically. Is there a way to find out the names of the unknown variables? For example:
foreach($value as $option){
$html .= "<input type='checkbox' name='".$key."[]' value='$option'>".htmlspecialchars($option)."</input>";
}
I need to know what the _POST['']
would be.
Upvotes: 1
Views: 1526
Reputation: 62412
Use the predefined variable $_POST and loop over :
foreach($_POST as $key => $value)
{
// $key will be the name
// $value will be the value of $_POST[$key]
}
Upvotes: 6
Reputation: 1609
You can loop over the post and get fields dynamically:
foreach($_POST as $key=>$value) {
echo "$key: $value\n";
}
You can do the same with $_GET.
Upvotes: 4
Reputation: 21272
There is also a function - get_defined_vars()
- that returns an array with all defined variables. Try the code below
$arr = get_defined_vars();
echo "<pre>"; print_r($arr);
Upvotes: 0
Reputation: 47776
You can loop through the $_POST
variable just like any other array
foreach($_POST as $key => $value)
echo "$key is $value";
Upvotes: 2
Reputation: 3095
You can name your checkboxes 'checkbox[$key][]' and iterate over $_POST[$key] using foreach
Upvotes: 3