-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
29 lines (26 loc) · 1.27 KB
/
Copy pathscript.js
File metadata and controls
29 lines (26 loc) · 1.27 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
//Required Elements
const form = document.querySelector("form");
statusTxt = form.querySelector(".button-area span");
form.onsubmit = (e)=>{
e.preventDefault(); //stops form from submitting
statusTxt.style.display = "block";
statusTxt.style.color = "red";
let xhr = new XMLHttpRequest(); //creating new XML object
xhr.open("POST", "message.php", true); //sending post message to message.php file
xhr.onload = ()=>{ //Once Ajazx is loaded
if (xhr.readyState == 4 && xhr.status ==200) { //if ajax response status is 200 & ready status is 4 means thers is no error
let response = xhr.response; //storing ajax response in a response variable
if (response.indexOf("Email and password field is required!") != -1 || response.indexOf("Enter a valid email address!") || response.indexOf("Sorry, failed to send your message")) {
statusTxt.style.color = "red";
}else{
form.reset();
setTimeout(()=>{
statusTxt.style.display = "none";
}, 3000);
}
statusTxt.innerText = response;
}
}
let formData = new FormData(form); //Creating new FormData obj. This obj is used to send form data
xhr.send(); //sending form data
}