Send radio box value with $_POST

2020-04-08 01:27发布

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!

6条回答
祖国的老花朵
2楼-- · 2020-04-08 01:35

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>
查看更多
▲ chillily
3楼-- · 2020-04-08 01:39

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

查看更多
看我几分像从前
4楼-- · 2020-04-08 01:42
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>';
}
查看更多
ゆ 、 Hurt°
5楼-- · 2020-04-08 01:45
<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>';
}
?>
查看更多
戒情不戒烟
6楼-- · 2020-04-08 02:00

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>
查看更多
趁早两清
7楼-- · 2020-04-08 02:01

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"];
}
查看更多
登录 后发表回答