-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_process.php
72 lines (55 loc) · 1.71 KB
/
form_process.php
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
$error = false;
// define variables and set to empty values
$name = $email = $message = "";
$name_error = $email_error = $message_error = "";
//Load the config file for connect to database
//form is submitted with POST method
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$name = $_POST['name'];
$email = $_POST["email"];
$message = $_POST["message"];
if(empty($_POST["name"])) {
$error = true;
$name_error = "First and last name cannot be empty!";
}else{
$name = $_POST['name'];
// check if name only contains letters and whitespace
if(!preg_match("/^[a-zšđčćžA-ZŠĐČĆŽ\s]*$/", $name)){
$name_error = "The first and last names can contain only letters and spaces!";
}
}
if(empty($_POST["email"])){
$email_error = "The e-mail can't be empty!";
}else{
$email = $_POST["email"];
// check if e-mail address is well-formed
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = "The e-mail address is incorrect!";
}
}
if(empty($_POST["message"])) {
$message_error = "The contents of the message cannot be empty!";
}else{
$message = $_POST["message"];
// check if name only contains letters and whitespace
if(!preg_match("/^[a-zšđčćžA-ZŠĐČĆŽ0-9 ,.!?\'\"]*$/", $message)){
$message_error = "The content of message cannot be special signs!";
}
}
if($name_error == '' && $email_error == '' && $message_error == ''){
//settings for PHPMailer
// save data to db
// load AJAX
if($error==false){
$data['response'] = "success";
$data['content'] = "Thanks " . ucwords($name) . ", I'll back to you soon!";
}
else
{
$data['response'] = "error";
$data['content'] = "An error occurred! Try again!";
}
echo json_encode($data);
}
}