【发布时间】:2016-07-22 00:54:06
【问题描述】:
我正在使用表单构建器,用户可以创建多个复选框部分。 当只有一个部分时,检查的值会很好地发送。
当有两个或多个不同的复选框部分时,发送的值会附加上一个复选框部分中先前检查的值,如下所示:
复选框部分 1 = 测试 1
复选框部分 2 = 测试 1,空调
复选框部分 3 = 测试 1、空调、第二个 1
换句话说,第二个和第三个复选框不应该包含之前的值。
我对此很接近,但我们将不胜感激。
这是相关的 HTML。你可以看到每个都有自己的数组:
HTML
Checkbox section 1
<input type="checkbox" name="txt5[]" value="test 1">test 1
<input type="checkbox" name="txt5[]" value="test 2">test 2
<input type="checkbox" name="txt5[]" value="test 3">test 3
<input type="checkbox" name="txt5[]" value="test 4">test 4
Checkbox section 2
<input type="checkbox" name="txt6[]" value="Air Conditioning">Air Conditioning
<input type="checkbox" name="txt6[]" value="Plumbing">Plumbing
<input type="checkbox" name="txt6[]" value="Mechanical">Mechanical
Checkbox section 3
<input type="checkbox" name="txt7[]" value="second 1">second 1
<input type="checkbox" name="txt7[]" value="second 2">second 2
<input type="checkbox" name="txt7[]" value="second 3">second 3
<input type="checkbox" name="txt7[]" value="second 4">second 4
PHP - 在下面的 PHP 代码部分,您可以看到一个检查它是否为数组然后循环遍历项目的部分。问题是它没有删除先前数组中先前检查的项目:
$str_mail_cont="<html>
<head>
<title></title>
</head>
<body style=\"background-color:#f1f2f2; margin-top:10px;\">
<table align=\"center\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\" style=\"background-color:#FFFFFF; border-top: none; font-family:Verdana; line-height:20px; font-size:9pt\" bordercolor=\"#111111\" width=\"95%\" id=\"AutoNumber1\">
<tr>
<td colspan='2' align=left><U><b>".$form_title."</b></U></td>
</tr>";
while($result_fld = mysql_fetch_array($rs_flds))
{
$amm = 'txt'.$num;
$fld_name=$result_fld['field_name'];
$temp = $_POST[$amm];
if(is_array($temp) && count($temp)>=1)
{
$str_mail_filed="<tr><td align='left' width='30%'><b>".$fld_name." :</b></td><td align='left'>";
$str_mail_cont.=$str_mail_filed;
for($k=0;$k<count($temp);$k++)
{
$chkbox_str .= $temp[$k].', ';
}
$chkbox_str = trim($chkbox_str, ', ');
$str_mail_cont .= $chkbox_str;
$str_mail_cont.="</td>";
}
else
{
$str_mail_cont.="<tr><td><b>".$fld_name." :</b></td><td align='left'> ".$temp."</td></tr>";
}
$num++;
}
$str_mail_cont.="</table>
</body>
</html>";
}
最终结果是它发送一封电子邮件:
Test Items : test 1
Reason(s) For Request : test 1Air Conditioning
More Tests : test 1Air Conditioningsecond 1
感谢您的帮助!
一些额外的信息..这是var_dump($temp)的结果
array(1) {
[0]=> string(6) "test 1"
}
array(1) {
[0]=> string(16) "Air Conditioning"
}
array(1) {
[0]=> string(8) "second 1"
}
【问题讨论】:
-
仅供参考
implode(),比你现在的做法好多了。$checkbox_str = implode(', ', $temp)。事实上,这可能会一起解决你最初的问题 -
我还建议您在开发环境中打开错误报告。你至少有一些
E_NOTICE级别的错误。在您的php.ini文件中;display_errors = On和error_reporting = E_ALL -
@Phil 我尝试替换 $chkbox_str .= $temp[$k].', ';使用 $chkbox_str = implode(', ', $temp) 我仍然得到相同的结果。