2014-03-31 162 views
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="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"; 

    } 

} 

?> 
+0

什么是确切的错误? – j08691

回答

3

你必须命名单选按钮都是一样的,然后给他们一个值,而不是:

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

嗯!如果按下时间或tredjetimesmaling,我怎样才能显示不同的输出?例如,如果按时间表,它应该显示你好,如果tredjetimesmaling被按下,它应该显示黄色。 – user3185936

+0

编辑我的答案为你 – Adam

+0

它显示你好两个。如果按tredjetimesmaling,则不是黄色。 – user3185936

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