Application Form (original)

[insert_php]
global $wpdb;

define(“SESSION_EMAIL_SURNAME”,”SESSION_EMAIL_SURNAME”);
define(“SESSION_EMAIL_FIRSTNAME”,”SESSION_EMAIL_FIRSTNAME”);

$emailTo = ‘info@whetstoneallotments.co.uk,betheridge@gmail.com’;
//$emailTo = ‘betheridge@gmail.com’;
$emailSubject = (‘Application to take over an allotment’);

$postData = (isset($_POST) ? $_POST: array());

$emailSent = $formResubmitted = false;

//print_r($_SESSION);exit;
if (isset($_SESSION[SESSION_EMAIL_SURNAME])
&& $_SESSION[SESSION_EMAIL_SURNAME]
&& $_SESSION[SESSION_EMAIL_SURNAME] == $postData[‘surname’]
&& isset($_SESSION[SESSION_EMAIL_FIRSTNAME])
&& $_SESSION[SESSION_EMAIL_FIRSTNAME]
&& $_SESSION[SESSION_EMAIL_FIRSTNAME] == $postData[‘first_name’]) {
// We have already presented the form and sent the email for this person, using first name/surname
$emailSent = true;
$formResubmitted = true;
}
$errors = array();

// Check on submit, unless we deem to have already sent the email
if ($postData
&& !$emailSent) {
// Validate form
$errors = getValidationErrors($postData);
if (!$errors) {
// Build and send email
if (wp_mail($emailTo, $emailSubject, getEmailMessage($postData))) {
// Tell ourselves that we have successfully sent the email to this person, first name/surname
$_SESSION[SESSION_EMAIL_SURNAME] = $postData[‘surname’];
$_SESSION[SESSION_EMAIL_FIRSTNAME] = $postData[‘first_name’];
$emailSent = true;
} else {
$errors[] = ‘An error was detected trying to email your details. Please contact support.’;
}
}
}

function getValidationErrors($data) {
$requiredFields = array(‘first_name’,’surname’,’address’,’postcode’,’telephone’,’email’);
$errors = array();
foreach ($data as $field => $value) {
if (in_array($field, $requiredFields)) {
if (!$value) {
$errors[$field] = ‘Please enter your ‘ . formatFieldName($field);
}
}
}
return $errors;
}

function getTheTitleSelect($selectedTitle) {
$titles = array(‘Mr’,’Ms’,’Mrs’,’Dr’,’Rev’);
$html = ‘‘;
return $html;
}

function getTheHowHearSelect($selectedHowHear) {
$howHears = array(
“Please select”=>””,
“Know an allotment holder”=>”Know an allotment holder”,
“Search engine”=>”Search engine”,
“Walking by”=>”Walking by”,
“Website”=>”Website”,
“Word of mouth”=>”Word of mouth”,
“Other”=>”Other”
);
$onChange = ‘onchange=”if (this.value==\’Other\’) { checkOther(); jQuery(\’#otherDetails\’).focus(); }”‘;
$html = ‘‘;
return $html;
}

function formatFieldName($fieldName, $ucFirst = false) {
if ($ucFirst) {
$fieldName = ucfirst($fieldName);
}
return str_replace(‘_’, ‘ ‘, $fieldName);
}

function getEmailMessage($data) {
$message = (“The following details were submitted by the applicant:\n\n”);
foreach ($data as $field => $value) {
if ($value) {
$message .= (formatFieldName($field, true) . ‘: ‘ . $value . “\n”);
}
}
return $message;
}

// ‘ ******************************************************** ‘;
// ‘ ******************************************************** ‘;
// ‘ ******************************************************** ‘;

print ‘

Apply for a Plot

‘;

if ($emailSent) {

print ”

Your details have been submitted to the allotment association secretary.”;
if (!$formResubmitted) {
// Add this so that, for testing, we can see that the application has detected that
// the form was re-submitted and that we did not re-send the same email
print ” Thank you.”;
}
print “

“;

} else {

print ‘

‘;

print ‘

[Details sent to: ‘ . $emailTo . “]

“;

print ‘* = A required field’;
print ‘

‘;

if ($errors) {
print (‘

‘);
foreach ($errors as $error) {
print (‘‘ . $error . ‘
‘);
}
print (‘

‘);
}

print ‘

‘;
print ‘

‘;
print ‘

‘;
print ‘

‘;
print ‘

‘;
print ‘

‘;
print ‘

‘;
print ‘

‘;
print ‘

‘;

print ‘

‘;
print ‘

‘;

print ‘

‘;
print ‘

‘;

print ‘

‘;
print ‘

‘;

print ‘

‘;

print ‘

‘;
print ‘

Title ‘ . getTheTitleSelect($postData[‘title’]) . ‘
First name*
Surname*
Address*
Postcode*
Telephone number*
Email*
Do you have any previous experience of gardening or allotments? If so please provide some detail:
Please outline your reasons for wanting an allotment:
How did you hear about Whetstone Stray allotments?: ‘ . getTheHowHearSelect($postData[‘howHear’]) . ‘
Other details

‘;
print ‘

‘;
print ‘

‘;

print ‘‘;
}
[/insert_php]