i have an html form full of text fields, checkbox’s , and radio fields.
i wanted to get all the names of the fields so that i can get started in validating the information in them.
the method i am using to get them is
if(isset($_POST['submit'])) {
foreach($_POST as $name => $value) {
print $name."<br/>";
}
}
but i noticed that it only displays textbox and textarea field names and it doesnt include checkbox and radio field names through this submission. do i need to include anything for it to grab the field names of those?
Checkboxes and radio buttons work a little differently than your standard inputs. If a checkbox is present on a form that doesn’t necessarily mean that it will be available in the resulting POST information. Rather, those values will only be avialable if they are actually marked (checkboxes checked and radio buttons selected). The proper way to test for their value in PHP is not to check the field value but rather to check
isset()first.For a checkbox:
and for a radio button:
To be a little more descriptive let’s say you have the following form:
If I were to submit that form with an email value of ‘test@email.com’ but not check the checkbox I would have the following in $_POST:
However, if I were to submit the same form with the same email address and check the checkbox I would have the following:
Hope that helps.