【问题标题】:PHP checkbox array post adding checked value to each successive arrayPHP复选框数组后添加检查值到每个连续的数组
【发布时间】: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 = Onerror_reporting = E_ALL
  • @Phil 我尝试替换 $chkbox_str .= $temp[$k].', ';使用 $chkbox_str = implode(', ', $temp) 我仍然得到相同的结果。

标签: php arrays loops checkbox


【解决方案1】:

我想通了。我只是在循环每个复选框数组的部分上方添加了一个变量声明,如下所示:

$chkbox_str ='';

现在可以正常工作了!

【讨论】:

  • 您应该将此标记为答案,因为它解决了您的问题。
猜你喜欢
  • 2013-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 2011-06-23
  • 2021-09-24
相关资源
最近更新 更多