Reputation: 173
Please help.
I am trying to dynamically name my checkboxes using php. I am using POST. The problem I am running in to is $element is not working. The results of $_POST does not show any checkboxes.
Thanks in advance for the help.
foreach(array_keys($cart_array) as $element)
{
print "<input type = 'checkbox' checked name = '{$element}' />";
}
But something like
foreach(array_keys($cart_array) as $element)
{
print "<input type = 'checkbox' checked name = '$element}' />";
}
works just fine. Notice the missing { near $element}. This code would show which checkboxes are on!! The printed array would have an extra "}"
Array
(
[Tomato_and_Cheese_small] => on
[Tomato_and_Cheese_small}] => 1
[Tomato_and_Cheese_large] => on
[Tomato_and_Cheese_large}] => 1
)
ps. there are other inputs like text that get posted to $_POST just fine. The print_r($cart_array) works fine too.
Upvotes: 2
Views: 426
Reputation: 50976
What happened was that he had checkboxes with the name $element and text fields with the same name too after that. So the checkbox name $element in $_POST got overrided by the text field $element
Upvotes: 0
Reputation: 99921
The browser sends the value of radio buttons only when they are checked.
Also, each radio button must have the same name (if you want to user to be able to check only one of them). Only the value changes:
print '<input type=checkbox checked value="'.htmlspecialchars($element).'" name=checked_items />';
POST this and check the value of $_POST['checked_items']
Upvotes: 1
Reputation: 7195
If a checkbox is not checked it will not be present in the post parameters.
Upvotes: 0