How can I send a radio or checkbox value to the $_POST array even when the field is empty?
\';
Should be :
HTML :
<form action="test.php" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="radio" name="gender" value="male"/>Test
<input type="submit" name="submit" value="submit"/>
</form>
PHP Code :
if(isset($_POST['submit']))
{
echo $radio_value = $_POST["radio"];
}
<form action="" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="radio" name="gender" checked value="male"> Male
<input type="radio" name="gender" value="femail"> Female
<input type="submit" value="submit">
</form>
<?php
if(isset($_POST)) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>
If you want to send blank value automatically then
By default checked that radio button and set blank value:
<form action="test.php" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="radio" name="gender" value='' checked>
<input type="submit">
</form>
Consider this example:
// Set a default value as male
<input type="radio" name="gender" checked value="male"> Male
<input type="radio" name="gender" value="femail"> Female
and you will get the value
Array
(
[name] =>
[email] =>
[gender]=>male
)
You only get the checked
radio in the $_POST
array
Try this it will work :
Unchecked radio
elements are not submitted as they are not considered as successful
. So you have to check if they are sent using the isset
or empty
function.
<?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" value="1"/>Male
<input type="submit" name="submit" value="submit"/>
</form>
HTML :
<form action="test.php" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="radio" name="gender" checked value="male"/>Male
<input type="radio" name="gender" value="female"/>Female
<input type="submit" name="submit" value="submit"/>
</form>
PHP :
if(isset($_POST)) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}