I would guess it's because when you use checkboxes, it is when you want to enable selecting multiple, and it's gonna be an array. PHP might want you to specify that it is an array. Unlike radio, where we don't use [], because it's only 1 value
If not add [ ] after "name", it only get last selected item only. That is the reason. $_POST is associative array, which has key, value pairs. Key: "foods" (the name attribute of the checkboxes) Value: An array of selected checkbox values (e.g., ["Pizza", "Hamburger"]).
I'll just put this in here: 5:36 In case some errors occur when you echo foods with numerical index, first make sure to check a box (or all boxes), then submit, the value/s should appear in the DOM. This happened to me just minutes ago, without checking the boxes first, a line of error occured but disappeared when I did the step above. I don't know the reason but the difference between the video and mine is Bro is using Windows and Chrome, I'm using Linux Mint and Firefox, mainly a speculation though.
Document
Pizza
Hamburger
Hotdog
Taco
Why do we add [] after foods when setting the name in HTML when we want them to be in the same group?
I would guess it's because when you use checkboxes, it is when you want to enable selecting multiple, and it's gonna be an array. PHP might want you to specify that it is an array. Unlike radio, where we don't use [], because it's only 1 value
If not add [ ] after "name", it only get last selected item only. That is the reason. $_POST is associative array, which has key, value pairs. Key: "foods" (the name attribute of the checkboxes)
Value: An array of selected checkbox values (e.g., ["Pizza", "Hamburger"]).
I'll just put this in here:
5:36
In case some errors occur when you echo foods with numerical index, first make sure to check a box (or all boxes), then submit, the value/s should appear in the DOM.
This happened to me just minutes ago, without checking the boxes first, a line of error occured but disappeared when I did the step above.
I don't know the reason but the difference between the video and mine is Bro is using Windows and Chrome, I'm using Linux Mint and Firefox, mainly a speculation though.
can you teach us advanced level of web programming