How can I send a radio or checkbox value to the $_POST array even when the field is empty?
<?php
if(isset($_POST)) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>
<form action="test.php" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="radio" name="gender">
<input type="submit">
</form>
Here is what I get from the page if I hit submit without even filling out data.
Array
(
[name] =>
[email] =>
)
As you see, without touching the input type text, their value was pushed to the $_POST array. How can I do this with the radio box? I essentially want to "set" it, although it is blank just like the text inputs.
I know I could always call
<?php
if(isset($_POST['gender'])) {
//Code
}
?>
But that's not necessarily what I'm looking for. I need it to set automatically. Thanks in advance!
If you want to send blank value automatically then
By default checked that radio button and set blank value:
Consider this example:
and you will get the value
You only get the
checked
radio in the$_POST
arrayTry this it will work :
Unchecked radio
elements are not submitted as they are not considered assuccessful
. So you have to check if they are sent using theisset
orempty
function.Should be :
HTML :
PHP Code :