-
-
Save dangayle/898bc5daa73af09e67b07e0ee60c96ab to your computer and use it in GitHub Desktop.
Formatted snippet authored by Jeff Richards (http://www.jrichards.ca/)
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
<html> | |
<head> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | |
<script> | |
function submitToAPI() { | |
var URL = ‘/contact’; | |
var data = { | |
name: $(‘#name-input’).val(), | |
email: $(‘#email-input’).val(), | |
description: $(‘#description-input’).val() | |
}; | |
$.ajax({ | |
type: ‘POST’, | |
url: URL, | |
dataType: ‘json’, | |
contentType: ‘application/json’, | |
data: JSON.stringify(data), | |
success: function () { | |
// clear form and show a success message | |
alert(‘yay’); | |
}, | |
error: function () { | |
// show an error message | |
alert(‘boo’); | |
} | |
}); | |
} | |
</script> | |
</head> | |
<body> | |
<form id="contact-form"> | |
<label for="name-input">Name:</label> | |
<input type="text" id="name-input" placeholder="name here…" /> | |
<label for="email-input">Email:</label> | |
<input type="email" id="email-input" placeholder="email here…"/> | |
<label for="description-input">How can we help you?</label> | |
<textarea id="description-input" rows="3" placeholder="tell us…"></textarea> | |
<button type="button" onClick="submitToAPI()">Submit</button> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment