简体   繁体   中英

radio buttons with if

Working on radio buttons but getting undefined index:

<form action='' method='Post'/>
Fra Dato: <input type="text" name="fraDato" value="<?php echo date('d-m-Y'); ?>" /> <br>
Til Dato: <input type="text" name="tilDato" value="<?php echo date('d-m-Y'); ?>"> <br> 
<input type="radio" name="timesmaling" value="1">Times malinger<br>
<input type="radio" name="tredjetimesmaling" value="1">Tredje times malinger <br>
<input type='submit' name='submit' value='Generer rapport'>

</form>

<?php
if (isset($_POST['submit'])) {

    if ($_POST['timesmaling'])

    {
        echo "timesmaling";
    }

    if ($_POST['tredjetimesmaling']) 
    {

        echo "tredjetimesmaling";

    }

}

?>

You have to name the radio buttons all the same, and then give them a value instead:

<input type="radio" name="group" value="timesmaling"> Times malinger
<input type="radio" name="group" value="tredjetimesmaling" checked> Tredje times malinger

<?php
if (isset($_POST['group']))
{
    echo $_POST['group']; // this is the value

    // You can do this now
    if($_POST['group'] == 'timesmaling')
    {
        echo 'hello';
    }
    elseif($_POST['group'] == 'tredjetimesmaling')
    {
        echo 'yellow';
    }
}

use like this..

<form action='' method='Post'/>
Fra Dato: <input type="text" name="fraDato" value="<?php echo date('d-m-Y'); ?>" /> <br>
Til Dato: <input type="text" name="tilDato" value="<?php echo date('d-m-Y'); ?>"> <br> 
<input type="radio" name="radio" checked value="timesmaling">Times malinger<br>
<input type="radio" name="radio" value="tredjetimesmaling">Tredje times malinger <br>
<input type='submit' name='submit' value='Generer rapport'>
</form>
<?php
if (isset($_POST['submit']))
{

    if (isset($_POST['radio']))
    {
        echo $_POST['radio'];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM