|
1 <?php |
|
2 if( isset($_POST) ){ |
|
3 |
|
4 //form validation vars |
|
5 $formok = true; |
|
6 $errors = array(); |
|
7 |
|
8 //sumbission data |
|
9 $ipaddress = $_SERVER['REMOTE_ADDR']; |
|
10 $date = date('d/m/Y'); |
|
11 $time = date('H:i:s'); |
|
12 |
|
13 //form data |
|
14 $name = $_POST['name']; |
|
15 $email = $_POST['email']; |
|
16 $telephone = $_POST['telephone']; |
|
17 $enquiry = $_POST['enquiry']; |
|
18 $message = $_POST['message']; |
|
19 |
|
20 //validate form data |
|
21 |
|
22 //validate name is not empty |
|
23 if(empty($name)){ |
|
24 $formok = false; |
|
25 $errors[] = "You have not entered a name"; |
|
26 } |
|
27 |
|
28 //validate email address is not empty |
|
29 if(empty($email)){ |
|
30 $formok = false; |
|
31 $errors[] = "You have not entered an email address"; |
|
32 //validate email address is valid |
|
33 }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){ |
|
34 $formok = false; |
|
35 $errors[] = "You have not entered a valid email address"; |
|
36 } |
|
37 |
|
38 //validate message is not empty |
|
39 if(empty($message)){ |
|
40 $formok = false; |
|
41 $errors[] = "You have not entered a message"; |
|
42 } |
|
43 //validate message is greater than 20 charcters |
|
44 elseif(strlen($message) < 20){ |
|
45 $formok = false; |
|
46 $errors[] = "Your message must be greater than 20 characters"; |
|
47 } |
|
48 |
|
49 //send email if all is ok |
|
50 if($formok){ |
|
51 $headers = "From: website@website.com" . "\r\n"; |
|
52 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; |
|
53 |
|
54 $emailbody = "<p>You have recieved a new message from the enquiries form on your website.</p> |
|
55 <p><strong>Name: </strong> {$name} </p> |
|
56 <p><strong>Email Address: </strong> {$email} </p> |
|
57 <p><strong>Telephone: </strong> {$telephone} </p> |
|
58 <p><strong>Enquiry: </strong> {$enquiry} </p> |
|
59 <p><strong>Message: </strong> {$message} </p> |
|
60 <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>"; |
|
61 |
|
62 mail("goran.freshmaker@gmail.com","New Enquiry",$emailbody,$headers); |
|
63 |
|
64 } |
|
65 |
|
66 //what we need to return back to our form |
|
67 $returndata = array( |
|
68 'posted_form_data' => array( |
|
69 'name' => $name, |
|
70 'email' => $email, |
|
71 'telephone' => $telephone, |
|
72 'enquiry' => $enquiry, |
|
73 'message' => $message |
|
74 ), |
|
75 'form_ok' => $formok, |
|
76 'errors' => $errors |
|
77 ); |
|
78 |
|
79 |
|
80 //if this is not an ajax request |
|
81 if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){ |
|
82 //set session variables |
|
83 session_start(); |
|
84 $_SESSION['cf_returndata'] = $returndata; |
|
85 |
|
86 //redirect back to form |
|
87 header('location: ' . $_SERVER['HTTP_REFERER']); |
|
88 } |
|
89 } |