05-12-2009, 08:11 PM
|
#8 (permalink)
|
|
The Contributor
Join Date: May 2009
Location: West Midlands
Posts: 26
Thanks: 2
|
PHP Code:
<?php #Script 3.0 - for check3.php
function valid_form(array $aFormData)
{
/* Set a default return that will be returned if validation fails. */
$aValidatedForm = array('status' => false);
/* The validation rules, specifying an optional regular expression to validate
that field. Fields without expressions will be ignored. */
$aValidationRules = array('name' => '[A-Za-z0-9\.|-|_/i]{2,20}', 'company' => '[A-Za-z0-9\.|-|_/i]{3,30}', 'address' => '[A-Za-z0-9\.|-|_/i]{3,50}', 'email' => '[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})', 'phone' => '(\s*\[?0\d{4}\]?\s*\d{6}\s*)|(\s*\[?0\d{3}\]?\s*\d{3}\s*\d{4}\s*)');
/* Loop through the validation rules specified above. We're going to be working in
the fashion of: exclude, include. Check all the exclusions first (those that return false).
If all is well at the end of the loop, return true as it passed! */
foreach ($aValidationRules as $szKey => $szRegex)
{
/* Check to see if the item even exists in the form data. */
if (!array_key_exists($szKey, $aFormData))
{
/* Specify a message so that the user knows what he or she did wrong. */
$aValidatedForm['message'] = $szKey . ' does not exist';
return $aValidatedForm;
}
/* Check to see if the item is simply empty (null, blank). */
if (empty($aFormData[$szKey]))
{
/* Do the same as above to inform the individual. */
$aValidatedForm['message'] = $szKey . ' cannot be empty';
return $aValidatedForm;
}
/* Check to see if we have specified a regular expression for this field. */
if (is_null($aValidationRules[$szKey]))
{
/* Continue the loop if not. This item passed validation if it got this far. */
continue;
}
/* Validate the value against the regular expression specified for this field at the top of
the function. */
if (!preg_match('~' . $aValidationRules[$szKey] . '~i', $aFormData[$szKey]))
{
/* Set the message as usual to notify them of this error. */
$aValidatedForm['message'] = $szKey . ' syntax is invalid';
return $aValidatedForm;
}
}
/* Everything passed and validated as we expected and so we can
return true knowing that if something failed in the form data, our script
wouldn't have gotten this far. */
$aValidatedForm['status'] = true;
return $aValidatedForm;
}
/* Our array that contains all the form data. Such form data fields can be
constructed using either PHP (Looping through _GET/_POST) or by naming
your form items as like: name="my_form[Name]" */
$aVariables = array
(
'name' => $_POST['name'],
'company' => $_POST['company'],
'address' => $_POST['address'],
'email' => $_POST['email'],
'phone' => $_POST['phone']
);
/* Validate the form using the values above. */
$aValidatedForm = valid_form($aVariables);
if (!$aValidatedForm['status'])
{
/* If the validation failed then display the message to the user. */
echo 'Oops: ' . $aValidatedForm['message'];
}
else
{
/* Otherwise... voila! */
echo '<p><b>Thankyou - your questionnaire has been sent. I will contact you within 24 hours.</b></p>';
$service = ($_POST['service']);
$reason = ($_POST['reason']);
$target = ($_POST['target']);
$pages = ($_POST['pages']);
$important = ($_POST['important']);
$name = ($_POST['name']);
$company = ($_POST['company']);
$address = ($_POST['address']);
$phone = ($_POST['phone']);
$email = ($_POST['email']);
$contact = ($_POST['contact']);
$comments = ($_POST['comments']);
$features = ($_POST['features']);
$feature_list = implode(", ", $features); //implode array from checkboxes ready for email
$to = 'me@domain.co.uk';
$subject = 'Quote Request';
$body = "You have recieved a quote request from $name at $company.\n They want you to $service a website. Their main reason for a website is $reason and their target audience is $target.\n They want about $pages pages on their website.\n They also feel $important is the most important element for their website. \n$name would like the website to have a $feature_list.\n They also added this: $comments . \n Their email address is $email, address: $address, and phone number: $phone .\n $name would prefer to be contacted by $contact.";
mail ($to, $subject, $body);
}
?>
There's the code. It all worked fine before I put it in one file ... (thanks to WildHoney :) )
|
|
|