help: can't get my checkboxes to display in email

Hi,
I m getting a bit frustrated as I have been trying to get my checkboxes to display in my email. The form is working and when checking in my email instead of having the checkboxes selected printing, it just prints array and I m not sure why. Any help would be greatly appreciated!

All this code is in one file.

code used below:
[php]
$product = trim($_POST[‘product’]);
$name = trim($_POST[‘name’]);
$location = trim($_POST[‘location’]);
$email = $_POST[‘email’];
$phone = trim($_POST[‘phone’]);
$contact = trim($_POST[‘contact’]); //checkbox
$message = trim($_POST[‘message’]);
$body .= “Name: “. $name .”\n\n”;
$body .= “Location: “. $location .”\n\n”;
$body .= “Email: “. $email .”\n\n”;
$body .= “Phone: “. $phone .”\n\n”;
$body .= “Would you prefer to be contacted: “. $contact .”\n\n”;
$body .= “Message: \n” .$message."\n\n\n";

$errors = array(); //Initialize error array

//checks for a name
if (empty($_POST[‘name’]) ) {
$errors[]=’• Please enter your name.’;
}

//checks for a location
if (empty($_POST[‘location’]) ) {
$errors[]=’• Please enter your location.’;
}

//checks for an email
if (empty($_POST[‘email’]) ) {
$errors[]=’• Please enter a valid email address.’;
} else {

if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['email'])))) {
$errors[]='• Please enter a valid email address';
}
}

//checks for checkboxes
if ( isset($_POST[‘contact’]) ) {
$_POST[‘contact’] = implode(’, ‘, $_POST[‘contact’]); //Converts an array into a single string
} else {
$errors[]=’• Please cselect at least one checkbox’;
}

//checks for a message
if (empty($_POST[‘message’]) ) {
$errors[]=’• Please enter your message.’;
}

// anti-spam trap: checks for the correct answer
if (empty($_POST[‘spamtrap’]) ) {
$errors[]=’• You did not answer the anti-spam question’;
} else {
// To mitigate the possibility of user errors, eliminate case-matching issues
$spamtrap = strtolower($_POST[‘spamtrap’]);
$answer = strtolower($answer);
// If $posted_var doesn’t match $answer
if($spamtrap != “$answer”) {
// Return an error or give value to a feedback/output variable
$errors[]=‘Anti-Spam Question/Answer Mismatch: The answer you supplied to the anti-spam question is incorrect. Correct answer is ‘.$answer.’. Please try again’;
}
}

if (empty($errors)) { //If everything is OK
//send an email
mail ($to, $product, $body, ‘From:’.$email);
echo ‘

Thank you for your enquiry!
Your message has been sent and we will be in contact with you as soon as possible.

’;
unset($_SESSION[‘myForm’]);
print_enquiry_form();
} else { //report the errors
echo ‘
’;
foreach ($errors as $msg) { //prints each error
echo “
$msg
\n
”;
}
echo ‘

’;
print_enquiry_form();
} //end of if(empty($errors)) IF
[/php]

HTML code:

<form action="<?php echo $_SERVER[’PHP_SELF’];?>" method="post">

			<label for="name" class="blank">Name <font color="#FF0000"><small>(required)</small></font></label>
            <span><input name="name" type="text" value="<?= $_SESSION['myForm']['name']; ?>" tabindex="1" /></span>
	    	<label for="location" class="blank">Location <font color="#FF0000"><small>(required)</small></font></label>
            <span><input name="location" type="text" value="<?= $_SESSION['myForm']['location']; ?>" tabindex="2" /></span>
	    	<label for="email" class="blank">Email <font color="#FF0000"><small>(required)</small></font></label>
            <span><input name="email" type="text" value="<?= $_SESSION['myForm']['email']; ?>" tabindex="3" /></span>
	    	<label for="phone">Phone</label>
            <span><input name="phone" type="text" value="<?= $_SESSION['myForm']['phone']; ?>" tabindex="4" /></span>
	    	<label for="product">Product</label>
            <span><input name="product" type="text" value="<?= $_SESSION['myForm']['product']; ?>" tabindex="5" /></span>
            <label for="contact" class="blank">Would you prefer to be contacted: <font color="#FF0000"><small>(required)</small></font></label>
            <span><input name="contact[]" type="checkbox" value="<?= $_SESSION['myForm']['by_email']; ?>" /> By email</span>
            <span><input name="contact[]" type="checkbox" value="<?= $_SESSION['myForm']['by_phone']; ?>" /> By phone</span>
            <span><input name="contact[]" type="checkbox" value="<?= $_SESSION['myForm']['morning']; ?>" /> Morning</span>
            <span><input name="contact[]" type="checkbox" value="<?= $_SESSION['myForm']['afternoon']; ?>" /> Afternoon</span>
            <span><input name="contact[]" type="checkbox" value="<?= $_SESSION['myForm']['anytime']; ?>" /> Anytime</span>
   			<label for="message" class="blank">Message <font color="#FF0000"><small>(required)</small></font></label>
            <textarea name="message" tabindex="7"><?= $_SESSION['myForm']['message']; ?></textarea><br />
            <label title="No worries, the text entered here is case-insensitive" for="spamtrap">Anti-Spam Question: <?php echo(''.$question.''); ?> <font color="#FF0000"><small>(required)</small></font></label>
            <span><input name="spamtrap" type="text" value="<?= $_SESSION['myForm']['spamtrap']; ?>" tabindex="8"/></span>
            <p><input type="submit" value="submit" name="submit" tabindex="9" /></p>
            <input type="hidden" name="submitted"  value="true" />
            
	    </form>

Thank you.

Hi,
Now I’m no php expert (at all) but I think your error is that in the form you have individual checkboxes and multiple checkboxes can be checked at once. So if I had 3 checkboxes (pretending those brackets are checkboxes):
[] I like milk.
[] I like cheese.
[] I like yougurt.
I could check both I like milk and I like cheese.
When the form is posting data, it is posting an array of trues and falses of which ones are checked and not. So if I checked both milk and cheese, the array would be:
true, true, false
The trues stand for the ones I checked (and in this case I checked milk and cheese which are the first two on the list, so the first 2 are true) and the only false one stands for the last one on the list that I didn’t check: yougurt. But the point is, these values are put into an array.
Now you tell me: if you try to print an array by using print “$arrayname” what do you get?
array
The reason why is that php doesn’t know what you want to do with the array. It’s like referring to a whole bunch of variables with one variable, and php wouldn’t know how to handle that.
Instead, you would have to loop through each element of the array, like this:
foreach ($arrayname as $a_variable_containing_data_about_the_array) {
print “$a_variable_containing_data_about_the_array”;
}
This will loop through each element of the array, starting at the first one and ending at the last, executing the same code each time it goes on to the next element, putting the current elements data into $a_variable_containing_data_about_the_array. You can then print these out one by one, as I have done here.

Same thing happening here. You have an array called $_POST[‘contacts’] with info about whether each checkbox is checked or not. The value of each element is true if it is checked, false if it is not checked, in order from top to bottom. When you try to print this to the body of the email message, all you get is “array”. You need to either:
loop through each element, adding it to the body if it is checked or
use radio buttons instead so that only one value can be chosen.
If you are using the first method, I would recommend looking into foreach.

I ll try what you re suggesting.

Thank you for your help!

Sponsor our Newsletter | Privacy Policy | Terms of Service