Handling Form Submission in React
Learn how to submit forms in React, handle form data, prevent page reloads, and display success messages after successful submission.
1. What Is Form Submission?
Form submission is the process of processing information entered by a user through a form. For example, a student registration form can collect a student's name, email address, and selected course.
React uses the onSubmit event to handle form
submission. When the user submits a form, the associated
event handler executes and processes the entered values.
event.preventDefault() when handling
submission in React without triggering the browser's
default form submission and page reload.
2. How Form Submission Works
A typical form submission process follows these steps:
The user enters information into the form fields. When the form is submitted, React calls the submit handler. The handler processes the values and updates the component's state to display the result.
3. Basic Form Submission Example
In this example, the user enters a name and clicks the Submit button. React displays a welcome message.
Example Code
import { useState } from "react";
function App() {
const [name, setName] = useState("");
const [message, setMessage] = useState("");
function handleSubmit(event) {
event.preventDefault();
setMessage(`Welcome, ${name}!`);
}
return (
<div>
<h2>Student Registration</h2>
<form onSubmit={handleSubmit}>
<label>Enter Name: </label>
<input
type="text"
value={name}
onChange={(event) =>
setName(event.target.value)
}
required
/>
<button type="submit">
Submit
</button>
</form>
<p>{message}</p>
</div>
);
}
export default App;
Expected Output
Student Registration
Enter Name: Samadhan
Button: Submit
Welcome, Samadhan!
handleSubmit() function executes when the
form is submitted. The preventDefault() method
prevents the default browser behavior, and
setMessage() updates the displayed message.
4. Handling Multiple Form Fields
A form may contain multiple input fields. We can use React state to store each input value and process all the values when the user submits the form.
Example Code
import { useState } from "react";
function App() {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [result, setResult] = useState("");
function handleSubmit(event) {
event.preventDefault();
setResult(
`Name: ${name}, Email: ${email}`
);
}
return (
<div>
<h2>Student Details</h2>
<form onSubmit={handleSubmit}>
<input
type="text"
placeholder="Enter name"
value={name}
onChange={(event) =>
setName(event.target.value)
}
required
/>
<br /><br />
<input
type="email"
placeholder="Enter email"
value={email}
onChange={(event) =>
setEmail(event.target.value)
}
required
/>
<br /><br />
<button type="submit">
Submit Details
</button>
</form>
<p>{result}</p>
</div>
);
}
export default App;
Expected Output
Name: Samiksha
Email: samiksha@example.com
After clicking Submit Details:
Name: Samiksha, Email: samiksha@example.comname and email state variables
store the input values. The submit handler reads these
values and displays them after submission.
5. Important Points About Form Submission
-
Use
onSubmitto handle form submission. -
Use
event.preventDefault()to prevent the browser's default form submission behavior. -
Use
type="submit"for the form submission button. - Use React state to store and display form values.
-
Use
requiredto make a field mandatory through basic browser validation. - Use an API or backend when submitted data must be saved to a server or database.
6. Practice Tasks
Practice the following tasks on your own:
- Create a form that accepts a student's name and displays a welcome message after submission.
- Create a form with name, email, and phone number fields. Display all the values after submission.
- Create a course registration form with student name and course name.
- Display a success message after the form is submitted.
- Make the name and email fields mandatory.
- Create a form that displays the submitted details without reloading the page.