带有if的单选按钮

时间:2014-03-31 13:18:30

标签: php html radio-button

使用单选按钮但获取未定义的索引:

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

    }

}

?>

2 个答案:

答案 0 :(得分:3)

您必须将单选按钮命名为all,然后为它们指定一个值:

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

答案 1 :(得分:0)

像这样使用..

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