Created
January 19, 2018 05:18
-
-
Save Gomah/a4bbc8c05bd0e603847b94ffb6a71ac1 to your computer and use it in GitHub Desktop.
Netlify form handling, based on https://www.netlify.com/blog/2017/07/20/how-to-integrate-netlifys-form-handling-in-a-react-app/
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
<template> | |
<form> | |
<p> | |
<label> | |
Your Name: <input type="text" name="name" v-model="form.name" /> | |
</label> | |
</p> | |
<p> | |
<label> | |
Your Email: <input type="email" name="email" v-model="form.email" /> | |
</label> | |
</p> | |
<p> | |
<label> | |
Message: <textarea name="message" v-model="form.message" /> | |
</label> | |
</p> | |
<p> | |
<button type="submit" @click.prevent="handleSubmit">Send</button> | |
</p> | |
</form> | |
</template> | |
<script> | |
export default { | |
data() { | |
return { | |
form: { | |
name: '', | |
email: '', | |
message: '', | |
}, | |
}; | |
}, | |
methods: { | |
encode(data) { | |
return Object.keys(data) | |
.map( | |
key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}` | |
) | |
.join('&'); | |
}, | |
handleSubmit() { | |
fetch('/', { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | |
body: this.encode({ 'form-name': 'contact', ...this.form }), | |
}) | |
.then(() => alert('Success!')) | |
.catch(error => alert(error)); | |
}, | |
}, | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment