在我的表单中,我试图将收音机选中的值传递到下一页(这是一个FPDF页面) 我有4种选择:年假,病假,商务假,&还有一些文本域。
然而,我尝试了很多'if'以及'switch cases' 我只得到值为'1'的元素 或者'未定义的索引:rad在第13行的D:\ xampp \ htdocs \ Application \ generate_report.php'
在某些我错的地方,任何人都可以帮助我。我的代码如下。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> <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 :(得分:1)
if($radio = 0)
和
elseif ($radio = 1)
所有其他的其他地方必须 == 1 ,两个'='!
答案 1 :(得分:1)
关于OP的进一步解释。如果你不使用==那么你正在设置值,而不是检查它。此外,还有一定程度的检查。使用double equals(==)实际上表示“等于”,而使用triple equals(===)就像声明“绝对等于”。通常,==运算符将执行您需要的所有操作,但有时在处理数据类型或您可能需要的特定值时===。这主要是因为OP有一个可行的解决方案。
答案 2 :(得分:0)
您应始终检查输入是否已选中或是否已插入任何值。如果没有值,则抛出未定义的索引错误。另外,您应该在if子句中将=
替换为==
。所以:
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';
}
现在应该可以了。