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 ‘
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 “
\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.