Last active
August 11, 2017 08:11
-
-
Save hygull/151ef218600afb4981dc991b494903bc to your computer and use it in GitHub Desktop.
Function that sends Contact Data to the backend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sendContactData() { | |
var fullname = document.forms["contactForm"]["fname"].value; | |
var email = document.forms["contactForm"]["email"].value; | |
var contact = document.forms["contactForm"]["contact"].value; | |
var message = document.forms["contactForm"]["message"].value; | |
// Make a post request using endpoint http://127.0.0.1:8000/contact/submit/ | |
fetch("{{ request.scheme}}://{{request.get_host}}/contact/submit/", { | |
method: "POST", | |
headers: { | |
"Accept": "application/json, text/html, */*", | |
"Content-Type": "application/x-www-form-urlencoded", | |
}, | |
body: JSON.stringify({ | |
fullname: fullname, | |
email: email, | |
contact:contact, | |
message: message | |
}) | |
}). | |
then(response => response.json()). | |
then(response => { | |
console.log("DATA:", response); | |
if(response.status == 200){ | |
alert(response.message) | |
console.log("Successful sent your contact details to Maprolubricants") | |
return true | |
} | |
alert(response.message) | |
console.log("Some error occured") | |
return false; | |
}). | |
catch(function(error){ | |
alert("Error => " + error.message) | |
return false; | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment