Send radio box value with $_POST

天大地大妈咪最大 提交于 2020-04-07 03:52:22

问题


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!


回答1:


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>



回答2:


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



回答3:


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:


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>



回答5:


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



回答6:


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


来源:https://stackoverflow.com/questions/27717181/send-radio-box-value-with-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!