在我的表单中,我试图将无线电检查值传递到下一页(即FPDF页)我有4种选择:年假,病假,商务假,&还有其他具有文本字段的。
然而,我已经尝试了很多"if"answers"switchcase"我只获取值为"1"的元素或'D:''xamplep''htdocs''Application''generate_report.php第13行中的未定义索引:rad'
有些地方我错了,有人能帮我吗。下面是我的代码。
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);
if($radio = 0)
和
elseif ($radio = 1)
所有其他eif必须是==1,带有两个'='!
关于OP的进一步解释。如果不使用==,则表示您正在设置值,而不是检查它。此外,还有检查级别。使用二重相等(==)实际上是在陈述"等于",而使用三重相等(===)就像在陈述"绝对等于"。通常情况下,==运算符会执行您需要的所有操作,但有时在处理数据类型或特定值时,您可能需要==。这主要是仅供参考,因为OP有一个可行的解决方案。
您应该始终检查是否检查了输入或插入了任何值。如果没有值,那么它会抛出一个未定义的索引错误。此外,您应该将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';
}
现在它应该起作用了。