Send radio box value with $_POST

前端 未结 6 1727
鱼传尺愫
鱼传尺愫 2021-01-19 11:10

How can I send a radio or checkbox value to the $_POST array even when the field is empty?

\';
            


        
相关标签:
6条回答
  • 2021-01-19 11:49

    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"];
    }
    
    0 讨论(0)
  • 2021-01-19 11:55
    <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 讨论(0)
  • 2021-01-19 12:01

    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>
    
    0 讨论(0)
  • 2021-01-19 12:07

    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

    0 讨论(0)
  • 2021-01-19 12:09

    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>
    
    0 讨论(0)
  • 2021-01-19 12:11
    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 讨论(0)
提交回复
热议问题