2013-11-23 76 views
0

在我的表单中,我试图将无线电检查值传递到下一页(这是一个FPDF页面) 我有4个选项:年假,病假,商务假期,&也其他人与文本框。传递单选按钮

不过,我已经尝试了很多的“如果”和“开关的情况下” 我越来越要么只用值“1” 否则元素“未定义指数:RAD在d:\ XAMPP \ htdocs中\ Application \ generate_report.php on line 13'

有些地方我错了,谁能帮助我。我的代码如下。

HTML表单:

<form id="formmain" method="post" action="generate_report.php" onsubmit="return_validate()"> 

<script type="text/javascript"> 

function selectRadio(n){ 

document.forms["form4"]["r1"][n].checked=true 

} 

</script> 


    <table width="689"> 
    <tr> 
     <td width="500d"> 
     <input type="radio" name="rad" value="0" /> 
     <label>Business Trip</label> 
     <input type="radio" name="rad" value="1"/><label>Annual Leave</label> 
     <input type="radio" name="rad" value="2"/><label>Sick Leave</label> 
     <input type="radio" name="rad" value="3"/><label>Others</label>&nbsp;<input type="text" name="others" size="25" onclick="selectRadio(3)" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 
     </td> 
    </tr> 
    </table> 
    //.... 


//below submit button is end of the html page: 
<input type="submit" name="submit" value="send" /> 
</form> 

生成PDF格式:

$radio = $_POST['rad']; // I am storing variable 
    if($radio = 0) { 
$type = 'Business Leave'; 
    }elseif ($radio = 1) { 
    $type = 'Annual Leave'; 
    }elseif ($radio = 2) { 
    $type = 'Sick Leave'; 
    } else { $type = $_POST['others']; } 
//echo 
$pdf->Cell(98,10, 'Reason | ' .$type , 1, 0, 'C', $fill); 

回答

0

您应该始终检查是否检查了输入或插入了任何值。如果没有值,则会抛出未定义的索引错误。另外,你应该在你的if子句中用= s代替==。所以:

PHP:

$radio = $_POST['rad']; // I am storing variable 

if (isset($radio)) { // checks if radio is set 

if($radio == 0) { 
    $type = 'Business Leave'; 
}elseif ($radio == 1) { 
    $type = 'Annual Leave'; 
}elseif ($radio == 2) { 
    $type = 'Sick Leave'; 
} else { 
    if (isset($_POST['others'])) { // cheks if input text is set 
    $type = $_POST['others']; 
    } 
    else { 
    echo 'Error'; 
    } 
} 
//echo 
$pdf->Cell(98,10, 'Reason | ' .$type , 1, 0, 'C', $fill); 
} 
else { 
echo 'Error'; 
} 

现在,它应该工作。

+0

我试过给了我一个意外的文件结尾的错误,并显示关闭php标签的错误。我不知道是不是因为我犯了错误'{' – sajeesh

+0

完美我完成了,并找到完美的工作,并得到了我想要的。我犯的错是pdf->格在'if'之外 – sajeesh

+0

很高兴听到:) – aksu

1
if($radio = 0) 

elseif ($radio = 1) 

和所有其他elseifs必须== 1,有两个'='!

+0

它给了我空白没有检索到价值.. – sajeesh

+0

它没有工作谢谢。 – sajeesh

1

对OP的进一步解释。如果您不使用==,那么您正在设置该值,而不是检查它。此外,还有一些检查级别。使用double等于(==)实际上等于“等于”,而使用三等于(===)就像声明“绝对等于”。通常,==运算符将完成您所需的所有工作,但有时在处理数据类型或特定值时可能需要===。由于OP具有可操作的解决方案,这主要是供参考。