I loathe forms. Wherever possible I try to avoid implementing them. In this case I have no choice and I'm left with an issue I can't figure out - this is based on Ryan's basic form example.
I'm stumped on how to send the check box values to the recipient email. It's probably glaringly obvious to most except me.
Regards
Marty
<?php
/**
* basic-form.php - Example of a simple contact form in ProcessWire
*
*/
// set this to the email address you want to send to (or pull from a PW field)
$emailTo = 'email@email.com';
// or if not set, we'll just email the default superuser
if(empty($emailTo)) $emailTo = $users->get($config->superUserPageID)->email;
// set and sanitize our form field values
$form = array(
'contact_name' => $sanitizer->text($input->post->contact_name),
'email' => $sanitizer->email($input->post->email),
'comments' => $sanitizer->textarea($input->post->comments),
);
// initialize runtime vars
$sent = false;
$error = '';
// check if the form was submitted
if($input->post->submit) {
// determine if any fields were ommitted or didn't validate
//foreach($form as $key => $value) {
// if(empty($value)) $error = "<p class='form-error'>Please completed all fields.</p>";
//}
//if(empty($form->$company_name)) $error = "<p class='form-error'>Please completed CF field.</p>";
// if no errors, email the form results
if(!$error) {
$subject = "A message from the contact form";
$message = '';
foreach($form as $key => $value) $message .= "$key: $value\n";
mail($emailTo, $subject, $message, "From: $form[email]");
$sent = true;
}
}
if($sent) {
echo "<p>Thank you, your message has been sent.</p>"; // or pull from a PW field
} else {
// encode values for placement in markup
foreach($form as $key => $value) {
$form[$key] = htmlentities($value, ENT_QUOTES, "UTF-8");
}
// output the form
echo <<< _OUT
$error
<form action="./" method="post" id="contact-form">
<p>
<label for="company_name" class="contact_form">Company name:</label>
<input type="text" id="company_name" class="contact_form " name="company_name" value="$form[company_name]" size="40" />
</p>
<p>
<label for="address_1" class="contact_form">Address 1:</label>
<input type="text" id="address_1" class="contact_form " name="address_1" value="$form[address_1]" size="40" />
</p>
<p>
<label for="address_2" class="contact_form">Address 2:</label>
<input type="text" id="address_2" class="contact_form " name="address_2" value="$form[address_2]" size="40" />
</p>
<p>
<label for="suburb" class="contact_form">Suburb</label>
<input type="text" id="suburb" class="contact_form " name="suburb" value="$form[suburb]" size="40" />
</p>
<p>
<label for="state" class="contact_form">State:</label>
<input type="text" id="suburb" class="contact_form " name="state" value="$form[state]" size="20" />
</p>
<p>
<label for="postcode" class="contact_form">Postcode:</label>
<input type="text" id="postcode" class="contact_form " name="postcode" value="$form[postcode]" size="10" />
</p>
<p>
<label for="country" class="contact_form">Country:</label>
<input type="text" id="postcode" class="contact_form " name="country" value="$form[country]" size="40" />
</p>
<p>
<label class="contact_form" for="contact_name">Contact person:*</label>
<input class="contact_form required" type="text" id="contact_name" name="contact_name" size="40" value="$form[contact_name]" />
</p>
<p>
<label for="position" class="contact_form">Position:</label>
<input type="text" id="position" class="contact_form " name="position" value="$form[position]" size="40" />
</p>
<p>
<label for="phone" class="contact_form">Phone</label>
<input type="text" id="phone" class="contact_form " name="phone" value="$form[phone]" size="40" />
</p>
<p>
<label class="contact_form" for="email">Your email:*</label>
<input class="contact_form required" type="email" name="email" id="email" size="40" value="$form[email]" />
</p>
<fieldset class="products">
<p>Products of interest:</p>
<fieldset class="prod_col">
<input type="checkbox" id="Hats" name="Hats" value="$form[hats]" /><label for="Hats" class="Hats">Hats</label><br/>
<input type="checkbox" id="Gloves" name="Gloves" value="$form[gloves]" /><label for="Gloves" class="Gloves">Gloves</label><br/>
<input type="checkbox" id="Mens" name="Mens" value="$form[mens]" /><label for="Mens" class="Mens">Mens</label><br/>
</fieldset>
<fieldset class="prod_col">
<input type="checkbox" id="Bags" name="Bags" value="$form[bags]" /><label for="Bags" class="Bags">Bags</label><br/>
<input type="checkbox" id="Scarves" name="Scarves" value="$form[scarves]" /><label for="Scarves" class="Scarves">Scarves</label><br/>
<input type="checkbox" id="Clothing" name="Clothing" value="$form[clothing]" /><label for="Clothing" class="Clothing">Clothing</label><br/>
</fieldset>
</fieldset>
<p>
<label class="contact_form comments" for="comments">If you have any further comments or questions, please complete this area:</label>
<textarea class="contact_form" id="comments" name="comments" rows="5" cols="62">$form[comments]</textarea>
</p>
<input class="contact_submit" type="submit" name="submit" value="Submit" />
<p>* required fields</p>
</form>
_OUT;
}
?>













