-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
238 lines (191 loc) · 8.46 KB
/
index.php
File metadata and controls
238 lines (191 loc) · 8.46 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- HTML Form -->
<form action="" class="contact-form clear" method="post" onSubmit="if (typeof formValidation !== 'undefined') { return formValidation(this) }">
<div class="row">
<label for='form-name' class='field-name'>Full Name <span class="color-red">*</span></label>
<div class="input-placeholder">
<input autocomplete="off" class="text input-md input-primary ignore-reset-js" maxlength="200" name="form-name" placeholder="Upesh Vishwakarma" type="text" />
</div>
</div>
<div class="row">
<label for='form-company' class='field-name'>Campany Name <span class="color-red">*</span></label>
<div class="input-placeholder">
<input autocomplete="off" class="text input-md input-primary" name="form-company" placeholder="Company" type="text" value="" />
</div>
</div>
<div class="row">
<label for='form-email' class='field-name'>Email <span class="color-red">*</span></label>
<div class="input-placeholder">
<input autocomplete="off" class="text input-md input-primary input-margin ignore-reset-js" name="form-email" placeholder="name@domain.com" type="text" />
</div>
</div>
<div class="row">
<label></label>
<div class="btn-contact input-placeholder">
<input class="contact-submit btn btn-primary btn-lg col-md-3" name="SubmitButtonModal" type="submit" value="Sign Up" />
</div>
</div>
</form>
<script>
// Form Validation
function formValidation() {
var ContactForm = {},
mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,15})+$/;
ContactForm.nameField = document.querySelector("[name='form-name']");
ContactForm.emailField = document.querySelector("[name='form-email']");
ContactForm.companyField = document.querySelector("[name='form-company']");
ContactForm.nameError = document.getElementById("name-error");
ContactForm.emailError = document.getElementById("email-error");
ContactForm.phoneError = document.getElementById("phone-error");
function form_validation(fieldName, fieldError, errorMessage, isEmail, emailErrorMessage) {
//Clear error field
fieldName.classList.remove("error-border");
if (fieldError) {
fieldError.remove();
}
if (!isEmail) {
if (fieldName.value == "") {
fieldName.classList.remove("error-border");
fieldName.insertAdjacentHTML('afterend', '<span class="error-message" id="name-error">' + errorMessage + '</span>');
fieldName.classList.add("error-border");
}
} else {
if (fieldName.value == "") {
fieldName.insertAdjacentHTML('afterend', '<span class="error-message" id="email-error">' + errorMessage + '</span>');
fieldName.classList.add("error-border");
} else if (!fieldName.value.match(mailformat)) {
fieldName.insertAdjacentHTML('afterend', '<span class="error-message" id="email-error">' + emailErrorMessage + '</span>');
fieldName.classList.add("error-border");
}
}
}
form_validation(ContactForm.nameField, ContactForm.nameError, 'please share your full name');
form_validation(ContactForm.emailField, ContactForm.emailError, 'please share your email address', true, 'please share your vaild email address');
form_validation(ContactForm.companyField, ContactForm.phoneError, 'please share name of your company ');
if (ContactForm.nameField.value == "" || ContactForm.emailField.value == "" || !ContactForm.emailField.value.match(mailformat) || ContactForm.companyField.value == "") {
return false;
} else {
return true;
}
}
</script>
<!-- Sending Form data to Mailchimp on Successful submission -->
<?php
if (isset($_POST['SubmitButtonModal'])) {
$email = sanitize_text_field($_POST['form-email']);
$formName = sanitize_text_field($_POST['form-name']);
$formPhone = sanitize_text_field($_POST['form-company']);
$status = 'subscribed'; // "subscribed" or "unsubscribed" or "cleaned" or "pending".
$tag = 'TagName'; // "TagName" is a tag of your form when we different for different form.
$list_id = 'XXXXXXXXXX'; // List id is also know as Audience ID.
$api_key = 'XXXXXXXXXX'; // API Key should always hide.
$merge_fields = array('FNAME' => $formName, 'ORG' => $formPhone);
mailchimp_subscriber_status($email, $status, $tag, $list_id, $api_key, $merge_fields, "");
}
?>
<?php
function mailchimp_subscriber_status($email, $status, $tag, $list_id, $api_key, $merge_fields = array())
{
$result = '';
$dc = substr($api_key, strpos($api_key, '-') + 1);
$data = array(
'apikey' => $api_key,
'email_address' => $email,
'status' => $status,
'tags' => array($tag),
'merge_fields' => $merge_fields,
);
/**
* Add a new list member
* API Ref : https://mailchimp.com/developer/reference/lists/list-members
* */
$url = 'https://' . $dc . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/';
$result = mailchimp_curl_connect($url, 'POST', $api_key, $data);
$json_array = json_decode($result, true);
// Check If memmber exists then update member tags
if ($json_array['title'] == 'Member Exists' && $json_array['status'] == 400) {
/**
* Update Merge fields
* API Ref : https://mailchimp.com/developer/reference/lists/list-members/#patch_/lists/-list_id-/members/-subscriber_hash-
* */
$data = array(
'merge_fields' => $merge_fields,
);
$url = 'https://' . $dc . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($email));
$result = mailchimp_curl_connect($url, 'PATCH', $api_key, $data);
/**
* Update Tags
* API Ref : https://mailchimp.com/developer/reference/lists/list-members/list-member-tags
* */
$data = array(
'tags' => array(
array(
'name' => $tag,
'status' => 'active',
),
),
'is_syncing' => false,
);
$url = 'https://' . $dc . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($email)) . '/tags';
$result = mailchimp_curl_connect($url, 'POST', $api_key, $data);
}
// Error if mailchimp is not successfull
if ($json_array['status'] != 400) {
if ($json_array['status'] != 'subscribed') {
echo "<script>
var mailchimp_err = 'MailChimp Integration Failed on page ' + window.location.href;
try{
throw new Error(mailchimp_err);
} catch (err) {
console.error(err);
}
</script>";
}
}
return $result;
}
// Common function for both GET and POST curl connect
function mailchimp_curl_connect($url, $request_type, $api_key, $data = array())
{
if ($request_type == 'GET') {
$url .= '?' . http_build_query($data);
}
$mch_api = curl_init();
// Basic Header Authentication
$headers = array(
'Content-Type: application/json',
'Authorization: Basic ' . base64_encode('user:' . $api_key),
);
/*
* Setting up useragent of HTTP Header for MailChimp Server
* Returing the API Response
*/
curl_setopt($mch_api, CURLOPT_URL, $url);
curl_setopt($mch_api, CURLOPT_HTTPHEADER, $headers);
if ($request_type != 'GET') {
curl_setopt($mch_api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
}
curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true);
// Based on requirement MailChimp API: POST/GET/PATCH/PUT/DELETE
curl_setopt($mch_api, CURLOPT_CUSTOMREQUEST, $request_type);
curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
// certificate verification for TLS/SSL connection
curl_setopt($mch_api, CURLOPT_SSL_VERIFYPEER, false);
if ($request_type != 'GET') {
curl_setopt($mch_api, CURLOPT_POST, true);
// send data in json
curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($data));
}
return curl_exec($mch_api);
}
?>
</body>
</html>