0

I'm working with a form builder and the user can create multiple sections of checkboxes. When there is just one section, the checked values get sent fine.

When there are two or more different checkbox sections, the sent value appends the previously checked value in the previous checkbox section like this:

Checkbox section 1 = test 1
Checkbox section 2 = test 1,Air Conditioning
Checkbox section 3 = test 1,Air Conditioning,second 1

In other words, the 2nd and 3rd checkboxes should not include the previous value.

I'm close on this, but any help would be greatly appreciated.

Here is the HTML for this. You can see each has its own array:

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 - In the part of PHP code below, you can see a section that checks if it is an array and then loops through the items. The problem is it's not removing the previously checked item in the previous array:

$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>";

}

The end result is it sends an email with this:

Test Items :    test 1
Reason(s) For Request : test 1Air Conditioning
More Tests :    test 1Air Conditioningsecond 1

Thanks for any help!

Some extra info.. This is a result of var_dump($temp)

array(1) {
    [0]=> string(6) "test 1"
}
array(1) {
    [0]=> string(16) "Air Conditioning"
}
array(1) {
    [0]=> string(8) "second 1"
}
3
  • 1
    FYI implode(), much better than the way you're doing it. $checkbox_str = implode(', ', $temp). In fact, this may solve your initial problem all together Commented Sep 10, 2013 at 1:20
  • 1
    I also suggest you turn on error reporting in your development environment. You have some E_NOTICE level errors at least. In your php.ini file; display_errors = On and error_reporting = E_ALL Commented Sep 10, 2013 at 1:21
  • @Phil I tried replacing $chkbox_str .= $temp[$k].', '; with $chkbox_str = implode(', ', $temp) and I still get the same result. Commented Sep 10, 2013 at 1:29

1 Answer 1

0

I figured this out. I simply added a variable declaration above the part where it loops each checkbox array like this:

$chkbox_str ='';

It works as expected now!

1
  • You should mark this as the answer as it solved your problem. Commented Jul 21, 2016 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.