Skip to content
Permalink
2627ee59e3
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
158 lines (113 sloc) 4.19 KB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact Form</title>
</head>
<body>
<form onsubmit="return false"; action="https://formspree.io/mgezqgzj" method="POST" class="form" id='myForm' >
<input type="text" onkeyup="formBlur(this)" id="username" placeholder="Name" name="username">
<p id="p-user"></p>
<input type="subject" onkeyup="formBlur(this)" id="subject" placeholder="Subject" name="subject">
<p id="p-subject" ></p>
<input type="email" onkeyup="formBlur(this)" id="email" placeholder="Email" name="email">
<p id="p-email" ></p>
<textarea id="textarea" onkeyup="formBlur(this)" rows="4" cols="40" placeholder="message" name="message" ></textarea>
<p id="p-textarea"></p>
<button onclick="actionForm()" id="my-form-button" >Submit</button>
<p id="my-form-status"></p>
</form>
<script>
function actionForm()
{
var username = document.getElementById('username'),
subject = document.getElementById('subject'),
email = document.getElementById('email'),
textarea = document.getElementById('textarea');
if(username.value == "")
{
document.getElementById('p-user').textContent = "* Please input your name";
document.getElementById('username').style.border = "1px solid red";
}
if(subject.value == "")
{
document.getElementById('p-subject').textContent = "* Please input your subject";
document.getElementById('subject').style.border = "1px solid red";
}
if(email.value == "")
{
document.getElementById('p-email').textContent = "* Please input your email";
document.getElementById('email').style.border = "1px solid red";
}
if(textarea.value == "")
{
document.getElementById('p-textarea').textContent = "* Please input your message ";
document.getElementById('textarea').style.border = "1px solid red";
}
}
function formBlur(input)
{
if(input.value != '')
{
input.nextElementSibling.textContent = "";
input.style.border = '2px solid green';
}
}
const encodeFormData = (data) => {
return Object.keys(data)
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(data[key]))
.join('&');
encodeFormData(formData);
};
const formDataToJson = formData => {
const entries = formData.entries();
const dataObj = Array.from(entries).reduce( (data, [key, value]) => {
data[key] = value;
if (key === 'email') {
data._replyTo = value;
}
return data;
}, {});
return JSON.stringify(dataObj);
};
const toggleForm = bool => {
FORM_ELEMENT.reset();
if (bool) {
FORM_ELEMENT.style.display = 'block';
SUCCESS_ELEMENT.style.display = 'none';
}
else {
FORM_ELEMENT.style.display = 'none';
SUCCESS_ELEMENT.style.display = 'block';
}
}
const YOUR_FORMSPREE_EMAIL = 'mtompkins09@yahoo.com';
const FORMSPREE_POST_URL = `https://formspree.io/mgezqgzj${ YOUR_FORMSPREE_EMAIL }`;
const FORM_ELEMENT = document.getElementById('myForm');
const SUCCESS_ELEMENT = document.getElementById('my-form-status');
FORM_ELEMENT.addEventListener('submit', function (e) {
e.preventDefault();
const formData = new FormData(this);
formData.append('_subject', 'Submission In Your Face');
fetch("https://formspree.io/mgezqgzj", {
method: 'POST',
body: formDataToJson(formData),
headers: {
'Content-Type': 'application/json'
}
})
.catch(e => {
})
.then(r => r.json())
.then(response => {
if (response.success === 'email sent') {
console.log(response);
toggleForm();
}
else {
}
});
});
</script>
</body>
</html>