-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
94 lines (89 loc) · 2.66 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
//Message Vars
$msg="";
$msgClass="";
//Check for submit
if (filter_has_var(INPUT_POST,'submit')) {
# Get Form Data
$name=htmlspecialchars($_POST["name"]);
$email=htmlspecialchars($_POST['email']);
$message=htmlspecialchars($_POST['message']);
//Check Required Fields
if(!empty($email) && !empty($name) && !empty($message)){
//Passed
//echo 'PASSED';
//check email
if(filter_var($email, FILTER_VALIDATE_EMAIL)===false){
//Failed
$msg='Please use a valid email';
$msgClass='alert-danger';
} else {
//Passed
//echo 'PASSED';
$toEmail = '[email protected]';
$subject = 'Contact Request From '.$name;
$body = '<h2>Contact Request</h2>
<h4>Name</h4><p>'.$name.'</p>
<h4>Email</h4><p>'.$email.'</p>
<h4>Message</h4><p>'.$message.'</p>
';
//Email headers
$headers = "MIME-Version:1.0" ."\r\n";
$headers .="Content-Type:text/html;charset=UTF-8" . "\r\n";
//Additional headers
$headers .= "From:" .$name. "<".$email.">". "\r\n";
if(mail($toEmail, $subject, $body, $headers)){
//Email sent
$msg = 'Your email has been sent';
$msgClass = 'alert-success';
}else{
//Email Failed
$msg = 'Your email was not sent';
$msgClass = 'alert-success';
}
}
} else {
//Failed
$msg='Please fill in all Fields';
$msgClass='alert-danger';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Contact Us</title>
<link rel="stylesheet" href="https://bootswatch.com/4/cosmo/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.php">Chunlin's Website</a>
</div>
</div>
</nav>
<div class="container">
<?php if($msg !=''): ?>
<div class="alert <?php echo $msgClass; ?>"><?php echo $msg; ?></div>
<?php endif; ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" value="<?php echo isset($_POST['name']) ? $name : ''; ?>
">
</div>
<div class="form-group">
<label>Email</label>
<input type="text" name="email" class="form-control" value="<?php echo isset($_POST['email']) ? $email : ''; ?>">
</div>
<div class="form-group">
<label>Message</label>
<textarea name="message" class="form-control"><?php echo isset($_POST['message']) ? $message : ''; ?></textarea>
</div>
<br>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</body>
</html>