Send the value to the radio camera using $ _POST

How can I send the radio or checkbox value to the $ _POST array even if 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>

      

This is what I get from the page if I hit submit without even filling in the data.

Array
(
    [name] => 
    [email] => 
)

      

As you can see, without touching the input type text, their value has been pushed into the $ _POST array. How can I do this using a radio camera? I essentially want to "set" it even though it's empty, 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 to install it automatically. Thanks in advance!

+3


source to share


6 answers


Try this, it will work:

The elements

Unchecked radio

are not sent as they do not count successful

. Therefore, you need to check if they are sent using a function isset

or 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" value="1"/>Male
    <input type="submit" name="submit" value="submit"/>
</form>

      

+1


source


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"];
}

      

+1


source


Consider the following 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 get the value

Array
(
    [name] => 
    [email] => 
    [gender]=>male
)

      

You get only radio station checked

in the array$_POST

+1


source


If you want to send an empty value automatically, then By default this radio button is checked and sets the empty 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>

      

0


source


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>';
}

      

0


source


<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>';
}
?>

      

0


source







All Articles