PHP Arrays and using them to send email from checkboxes


I have built an HTLM form with checkboxes. I am sending the output to email. I am having trouble understanding how to cleanup the array I’m using to make the output from the 8 possible checkboxes look good in the email to be sent. For example if I have

Trash Pickup

These are all as check boxes and assuming that the user selects just a few boxes. How do I output in the body of the email just those selected with a comma separator.

At this point I’m using $array[0] $array[1] etc…thru [7]

I end up with spaces between for those not selected like
" Sidewalks Taxes Police"
I would like to test the array and append commas so I can get nice clean output like
Sidewalks, Taxes, Police

Any Help would be great!


What exactly do your checkboxes look like? Are they set up like this:

<input type="checkbox" name="myOption[0]" value="Streets"> Streets

Then you could check each value in your array against the given value in the form element, as non-checked boxes return no value (their $_POST indexes don’t even exist):

if (isset($array[0]) OR $array[0] == “Streets”) {
$mySelection .= ", ".$array[0];

The if statement should already break upon the first condition if the checkbox wasn’t set, thus bypassing the $mySelection concatenation.


My code does look like that

input type=“checkbox” name=“myOption[0]” value=“Streets”> Streets
one for each checkbox [1] checkbox [2] etc…

The code you suggest would seem to be testing only one postiion in the array.

if (isset($array[0]) OR $array[0] == “Streets”) {
$mySelection .= ", ".$array[0];
} [/color]

How do I get this code to ‘loop’ through all positions in the array? So that $mySelection is filled with all the checkbox results selected?

Here is my code to contruct the email

$messageproper =

"This message was sent from:n" .
"$http_referrern" .
"------------------------------------------------------------n" .
"Name of sender: $namen" .
"Email of sender: $emailn" .
"Company: $companyn".
"Address: $addressn".
"City: $cityn". "State: $staten". "Zip: $zipn".
"Phone: $phonen".
"Web: $webn".
"Issues: $issuesn".

"nn------------------------------------------------------------n" ;

mail($mailto, $subject, $messageproper,
“From: “$name” <$email>” . $headersep . “Reply-To: “$name” <$email>” . $headersep . “X-Mailer: chfeedback.php 2.07” );
header( “Location: $thankyouurl” );
exit ;

Thank you for your help. Sorry I may be missing your suggestion. I have done some PHP but not checkboxes or radio buttons.



PHP Control Structures :slight_smile: Pick your favorite: while, for, foreach, do-while.