📖 Scenario: You are building a simple contact form on a website. When a user fills in their name and message, the form should send this data to a server using a POST request.
🎯 Goal: Create a Vue component that collects user input for name and message, then sends this data as a POST request when the form is submitted.
📋 What You'll Learn
Use Vue 3 Composition API with <script setup>
Create reactive variables for form data
Add a function to handle form submission
Send form data as JSON using fetch with POST method
Bind form inputs to reactive variables
Use semantic HTML form elements with accessibility in mind
💡 Why This Matters
🌍 Real World
Forms are everywhere on websites and apps. Sending data with POST requests is how user input reaches servers for processing.
💼 Career
Understanding form submission and POST requests is essential for frontend developers working with APIs and building interactive web applications.
Progress0 / 4 steps
1
Set up reactive form data
Inside a <script setup> block, import ref from 'vue' and create two reactive variables called name and message, both initialized as empty strings.
Vue
Hint
Use import { ref } from 'vue' and then const name = ref('') and const message = ref('').
2
Add a submit handler function
Add a function called submitForm inside the <script setup> block. This function should prevent the default form submission behavior by accepting an event parameter and calling event.preventDefault().
Vue
Hint
Define function submitForm(event) and call event.preventDefault() inside it.
3
Send form data with POST request
Inside the submitForm function, add code to send a POST request to 'https://example.com/api/contact' using fetch. The request should have headers specifying 'Content-Type': 'application/json' and the body should be a JSON string containing name.value and message.value. Use await and mark submitForm as async.
Vue
Hint
Use await fetch with method 'POST', headers for JSON, and body: JSON.stringify({ name: name.value, message: message.value }).
4
Bind inputs and connect submit handler
In the , add two labeled inputs: a text input bound with v-model to name and a textarea bound with v-model to message. Add a submit button with the label 'Send'. Attach the submitForm function to the form's @submit event.
Vue
Hint
Use v-model on inputs and connect @submit="submitForm" on the form.
Practice
(1/5)
1. In Vue, which directive is used to prevent the default form submission and handle it with a method instead?
easy
A. @input.prevent
B. @click.prevent
C. v-model.prevent
D. @submit.prevent
Solution
Step 1: Understand Vue event modifiers
Vue provides event modifiers like .prevent to stop default browser actions.
Step 2: Identify the correct event for form submission
The form submission event is submit, so @submit.prevent stops the default submit and allows custom handling.
Final Answer:
@submit.prevent -> Option D
Quick Check:
Use @submit.prevent to control form submit [OK]
Hint: Use @submit.prevent to stop default form submit [OK]
Common Mistakes:
Using @click.prevent on form instead of @submit.prevent
Trying to use v-model.prevent which doesn't exist
Using @input.prevent which is for input events
2. Which of the following is the correct way to send a POST request with form data using Vue's fetch API inside a method?
easy
A. fetch('/api', { method: 'POST', body: JSON.stringify(data) })
B. fetch('/api', { method: 'GET', body: JSON.stringify(data) })
C. fetch('/api', { method: 'POST', data: data })
D. fetch('/api', { method: 'POST', body: data })
Solution
Step 1: Identify the correct HTTP method for sending data
POST is used to send data to the server, so method must be 'POST'.
Step 2: Use the correct property for sending JSON data
fetch expects the data in the body property as a string, so use JSON.stringify(data).
Final Answer:
fetch('/api', { method: 'POST', body: JSON.stringify(data) }) -> Option A
Quick Check:
POST with JSON.stringify in body = fetch('/api', { method: 'POST', body: JSON.stringify(data) }) [OK]
Hint: Use method POST and JSON.stringify(data) in body [OK]
Common Mistakes:
Using method GET when sending data
Passing data directly without JSON.stringify
Using 'data' property instead of 'body'
3. Given this Vue method for form submission, what will be logged if the server responds with status 200?
fetch expects the body to be a string for JSON data, so this.formData must be stringified.
Step 2: Identify missing JSON.stringify
Without JSON.stringify(this.formData), the server may not receive data correctly.
Final Answer:
Missing JSON.stringify for body data -> Option A
Quick Check:
Body must be JSON string, not raw object [OK]
Hint: Always stringify JSON data before sending in fetch body [OK]
Common Mistakes:
Sending raw object instead of JSON string
Forgetting method: 'POST' (not the case here)
Not handling errors with catch
5. You want to submit a form in Vue and show a success message only if the POST request returns JSON with { success: true }. Which code snippet correctly implements this?
hard
A. async submit() {
const res = await fetch('/submit', { method: 'POST', body: JSON.stringify(this.form) });
const data = await res.json();
if (data.success) alert('Submitted!');
}
B. async submit() {
const res = await fetch('/submit', { method: 'POST', body: this.form });
const data = await res.json();
if (data.success) alert('Submitted!');
}