OK. Here it is:
My first bit of code is in a file that only calls the various functions. Here it is:
[php]<?php
include(‘includes/contact_form.inc.php’);
?>[/php]
Contact Information
Please use the convenient form below to contact us.
[php]<?php
switch($_GET[action]) {
case "check";
$errors=checkForm();
if($errors!="ok") {
// print “
”;print_r($errors);print “
”; // using for checking the values in the errors array.
echo ’
Error!
The following error(s) occurred:
’;
foreach ($errors as $message) { // Print each error individually.
echo " - $message
\n";
}
echo ‘
Please try again.
’;
emailForm();
} else {
sendEmail();
}
break;
default:
emailForm();
break;
}
?>
[/php]
The second file is called from the first bit of code above and contains all of the form formatting and the validation as well as sending the form data to an email address. I need to send the form data to an external url in addition to sending it to the email address:
[php]
<?
//contact_form.inc.php
function emailForm() {
print "
- Who Should We Contact?
-
First Name:
-
Last Name:
-
-
E-mail address:
-
Phone:
-
Alternate Phone:
-
Fax:
-
Street address 1:
-
Street address 2:
-
City:
-
State:
-
Zip Code:
- Additional Information
-
Arrival Date:
Unknown
January
February
March
April
May
June
July
August
September
October
November
December
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2011
2012
2013
2014
2015
-
Departure Date:
Unknown
January
February
March
April
May
June
July
August
September
October
November
December
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2011
2012
2013
2014
2015
- What Information Would You Like?
-
Brochure & Rates for the Hotel
Information about The Conference Center
Information for family reunions, church or school events
Information on conference and business meetings
- Your questions or comments.
-
$_POST[Message]
-
- Required Fields are marked with
";
}
function checkForm() {
$errors=array(); //Initialize array.
if(strlen($_POST[FirstName]) < 1) { //Check that field is filled in.
$errors[] = "Please enter your first name."; }
if(strlen($_POST[LastName]) < 1) { //Check that field is filled in.
$errors[] = "Please enter your last name."; }
if(strlen($_POST[HomePhone]) < 1) { //Check that field is filled in.
$errors[] = "Please enter your phone number."; }
if(strlen($_POST[EmailAddress]) < 1) { //Check that field is filled in.
$errors[] = "Please provide your email address."; }
if(strlen($_POST[Address1]) < 1) { //Check that field is filled in.
$errors[] = "Please provide your street address."; }
if(strlen($_POST[City]) < 1) { //Check that field is filled in.
$errors[] = "Please provide your city."; }
if(strlen($_POST[State]) < 1) { //Check that field is filled in.
$errors[] = "Please provide your state."; }
if(strlen($_POST[ZipCode]) < 1) { //Check that field is filled in.
$errors[] = "Please provide your zip code."; }
if(strlen($_POST[Message]) < 1) { //Check that field is filled in.
$errors[] = "You did not enter a message in the 'message' text box."; }
if($_POST[surname] !='') { //Check whether field is filled in. If it is, then don't send the email.
exit(); }
if(checkEmail($_POST[EmailAddress]) == "bad email") { //Check that email is in the correct form using checkEmail function.
$errors[] = "Your email address is not valid. Enter a complete email address using the format:
[email protected].";}
if(checkMessage($_POST[Message]) == "bad message") {//Check for urls in the message content.
$errors[] = "No Website URLs are permitted in the text field. If you need to send a link please do so without using the 'http://'. Just begin your link with 'www'.";
}
if (empty($errors)) { // if errors array is empty.
return "ok";
} else {
return $errors;
}
// print "
";var_dump($errors);print "
";
} // end function checkForm.
function checkEmail($email) {
if(!eregi("^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,6}\$",$email)) { return "bad email";
}
}
function checkMessage($message) {
if (preg_match("/http/i",$message))
{
return "bad message";
exit();
}
}
function sendEmail() {
$product = $_POST['product'];
if($product) {
foreach ($product as $p)
$products = implode( "\n", $product);
}
else
{
$products="No information selected.";
}
$contact = $_POST['contact'];
$from = $_POST['email'];
$body= "A request for information has been submitted from the Website:\n\n";
$body.="CONTACT INFORMATION\n\n";
$body.="First Name: $_POST[FirstName]\n";
$body.="Last Name: $_POST[LastName]\n";
$body.="Email: $_POST[EmailAddress]\n";
$body.="Phone Number: $_POST[HomePhone]\n";
$body.="Cell Phone: $_POST[CellPhone]\n";
$body.="Fax: $_POST[fax]\n\n";
$body.="ADDRESS\n\n";
$body.="Street1: $_POST[Address1]\n";
$body.="Street2: $_POST[Address2]\n";
$body.="City: $_POST[City]\n";
$body.="State: $_POST[State]\n";
$body.="Zip Code: $_POST[ZipCode]\n\n";
$body.="------------------------------------------------------------------\n\n";
$body.="Arrival Date: $_POST[month1] $_POST[day1], $_POST[year1]\n";
$body.="Departure Date: $_POST[month2] $_POST[day2], $_POST[year2]\n\n";
$body.="------------------------------------------------------------------\n\n";
$body.="REQUESTED INFORMATION:\n\n";
$body.="{$products}\n\n";
$body.="------------------------------------------------------------------\n\n";
$body.=stripslashes("MESSAGE: $_POST[Message]\n");
$headers = "From: ".$from;
$ok = @mail('
[email protected]', 'Information inquiry from the Website', $body, $headers);
if ($ok) {
print "
![thank you graphic]()
";
} else {
print "
Mail could not be sent. Sorry! Please try again. ";}
}
?>[/php]
Thanks so much.