Skip to content

Instantly share code, notes, and snippets.

@Gomah
Created January 19, 2018 05:18
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gomah/a4bbc8c05bd0e603847b94ffb6a71ac1 to your computer and use it in GitHub Desktop.
Save Gomah/a4bbc8c05bd0e603847b94ffb6a71ac1 to your computer and use it in GitHub Desktop.
<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