Hi Everyone,
I have used PHP Solutions by David Powers to put together a contact form for my web site but I can't get the items in the checkbox to show in my email message. Please could anyone suggest what I need to do to get this to work. I have read the book over and over and tried lots of variations. The book has examples to download but none of these explain or show in simple terms what to do.
Here is the code :
<?php
if (array_key_exists('send', $_POST)) {
$to ='ronwoodtd@gmail.com';
$subject ='Party Enquiry';
$headers='webmaster@jollygoodfun.co.uk';
//list expected fields
$expected = array('first_name', 'last_name', 'telephone', 'email', 'comments', 'entertainment');
// set required fields
$required =array('first_name', 'last_name', 'telephone', 'email', 'entertainment');
// create empty array for missing fields
$missing =array();
// assume nothing is suspect
$suspect = false;
// create a pattern to locate suspect phrases
$pattern = '/Content-Type:|Bcc:|Cc:/i';
// function to check for suspect phrases
function isSuspect($val, $pattern, &$suspect) {
// if the variable is an array, loop through each element
// and pass it recursively back to the same function
if (is_array($val)) {
foreach ($val as $item) {
isSuspect($item, $pattern, $suspect);
}
} else {
// if one of the suspect phrases is found, set Boolean to true
if (preg_match($pattern, $val)) {
$suspect = true;
}
}
}
if ($suspect) {
$mailSent = false;
unset($missing);
}
else {
/* process the variables */
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
$missing[] = $key;
} elseif (in_array($key, $expected)) {
// otherwise, assign to a variable of the same name as $key
${$key} = $temp;
}
}
}
// go ahead only if not suspect and all required fields ok
if (!$suspect && empty($missing)) {
/* build the message */
$message = "First Name: $first_name\n\n";
$message .= "Last Name: $last_name\n\n";
$message .= "Telephone: $telephone\n\n";
$message .= "Email: $email\n\n";
$message .= "Comments: $comments\n\n";
$message .= "Entertainment: $entertainment";
/* limit line length to 70 characters */
$message =wordwrap($message, 70);
/* send it */
$mailSent = mail($to, $subject, $message, $headers);
if ($mailSent) {
// $missing is no longer needed if email is sent, so unset it
unset($missing);
}
}
}
?>
<?php
require ('templates/docmettacss.html');
?>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="Send an enquiry to Jolly Good Productions using the form on this page." />
<meta name="keywords" content="enquiry form, enquiry, enquire to Jolly Good Productions" />
<title>Request a quotation from jolly Good Productions</title>
</head>
<?php
require('templates/header.html');
?>
<h1>Request a quotation</h1>
<?php
if ($_POST && isset($missing) && !empty($missing)) {
?>
<p style="color: red">Please complete the missing item(s) indicated.</p>
<?php
}
elseif ($_POST && !mailSent) {
?>
<p style="color: red">Sorry, there was a problem sending your message, please try later.</p>
<?php
}
elseif ($_POST && $mailSent) {
?>
<p><strong>Your message has been sent. Thank you for your enquiry.</strong></p>
<?php } ?>
<div id="contact_form"> <!-- div containing the contact form -->
<form id="feedback" method="post" action="">
<p>
<label for="first_name">First Name: <?php
if (isset($missing) && in_array('first_name', $missing)) { ?>
<span style="warning">Please enter your first name</span><?php } ?>
</label>
<input name="first_name" id="first_name" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['first_name']).'"';
} ?>
/>
<label for="last_name">Last Name: <?php
if (isset($missing) && in_array('last_name', $missing)) { ?>
<span class="warning">Please enter your last name</span><?php } ?>
</label>
<input name="last_name" id="last_name" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['last_name']).'"';
} ?>
/>
<label for="telephone">Telephone: <?php
if (isset($missing) && in_array('telephone', $missing)) { ?>
<span class="warning">Please enter your telephone number</span><?php } ?>
</label>
<input name="telephone" id="telephone" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['telephone']).'"';
} ?>
/>
<label for="email">Email: <?php
if (isset($missing) && in_array('email', $missing)) { ?>
<span class="warning">Please enter your Email</span><?php } ?>
</label>
<input name="email" id="email" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['email']).'"';
} ?>
/>
<label for="comments">Comments: <?php
if (isset($missing) && in_array('comments', $missing)) { ?>
<span class="warning">Please enter your comments</span><?php } ?>
</label>
<textarea name="comments" id="comments" cols="60" rows="8">
<?php
if (isset($missing)) {
echo htmlentities($_POST['comments']);
} ?></textarea>
</p>
<fieldset id="entertainment">
<h2>Entertainment Required</h2>
<div>
<p><input type="checkbox" name="entertainment[]" value="Chuckle The Clown" id="clown"
<?php
$OK = isset($_POST['entertainment']) ? true : false;
if ($OK && isset($missing) && in_array('Chuckle The Clown', $_POST['entertainment'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="clown">Chuckle The Clown</label>
</p>
<p><input type="checkbox" name="entertainment[]" value="Magic Party" id="magic"
<?php
if ($OK && isset($missing) && in_array('Magic Party', $_POST['entertainment'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="magic">Magic Party</label>
</p>
<p><input type="checkbox" name="entertainment[]" value="Punch and Judy" id="punch"
<?php
if ($OK && isset($missing) && in_array('Punch and Judy', $_POST['entertainment'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="punch">Punch and Judy</label>
</p>
<p><input type="checkbox" name="entertainment[]" value="Pirate Party" id="pirate"
<?php
if ($OK && isset($missing) && in_array('Pirate Party', $_POST['entertainment'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="pirate">Pirate Party</label>
</p>
<p><input type="checkbox" name="entertainment[]" value="Magic and Puppets" id="puppets"
<?php
if ($OK && isset($missing) && in_array('Magic and Puppets', $_POST['entertainment'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="puppets">Magic and Puppets</label>
</p>
<p><input type="checkbox" name="entertainment[]" value="Hazels Jolly Good Fun Party" id="hazel"
<?php
if ($OK && isset($missing) && in_array('Hazels Jolly Good Fun Party', $_POST['entertainment'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="hazel">Hazels Jolly Good Fun Party</label>
</p>
</div>
</fieldset>
<p>
<input name="send" id="send" type="submit" value="Send message" />
</p>
</form>
</div> <!-- End of div id contact form -->
<p class="home">Go to back to <a href="index.php">Home</a> </p>
<?php
require('templates/footer.html');
?>
and this is what it produces as the email:
webmaster@jollygoodfun.co.uk
First Name: John
Last Name: Smith
Telephone: 0123 456789
Email: john@gonehome.com
Comments: test again
Entertainment: Array
Any help would be greatly appreciated.
Regards
Ron


