How to Post/Send form data from React to backend API and save data in MongoDb Database — Handle ReactJS Form Submit to Database Example
tags: save form data to database,react form submission,react hook form,react form,react bootstrap form,react form hook,react form validation,react form submit,react submit form,react form submit example,react input,mongodb,mongoose,axios,how to post form data to api with react js,post api javascrit,post and get api,submit form data with react js,react save form data to mongodb,formik,react final form,form validation in react js,user signup,user registration form
In this video we’re gonna save form data from react js into mongodb database. we have already done input validation for this form in my last video. If you want to learn how to create and validate this form then I highly recommend you watch my last video on my youtube chanel. youtube.com/codingMoney and find video titled React Bootstrap form validation example. It was actually one of my subscriber Anshul Manapure who requested me to make a video where you send data to database through backend. BTW, I read and respond to all the comments so feel free to leave me a comment. by the end of this video, you will be able to handle form submit, POST data to the end point API and perform backend data validation and then saving data into mongodb using mongoose. so lets get started!
Before we start coding, lets take a look at this diagram and really grasp the concept of what we wanna do here. so after the user fills out the form and as soon as the user clicks submit button a function will run called handleSubmit, this function checks if the input data is valid and if it is, I wanna call a function called registerUser(formData) with the form data passed into it as an object.
this function will post the request to the backend api endpoint /api/users/register
In the mean time we will show a loading icon to let the user know that the request is being handled. here at the backend We will have a specific function that will handle this request and perform further server side validation , if the data is valid we will use mongoose to save the data into mongodb database and show a success message or redirect user to another page, if the request fails due to some reason we will let the user know by showing the error.
So basically we will handle 3 types of action , we will dispatch USER_REGISTER_REQUEST as soon as the user clicks the submit button
will dispatch success incase the request was successful
USER register fail incase the request failed
now lets start coding.
dispatch(registerUser(formData)) special function react-redux function.. redux is a seperate topic, which is beyond the scope of this video. i will explain it a bit but won’t go into details otherwise the video will be too long.
This where we left off in my last video, check that video out if you wanna follow along, i will leave card link on the screen so that you can easily find it.
This is the handleSubmit which is invoked as soon as the user clicks the submit button, it checks for errors and if there are no errors we can submit the form by creating a function called registerUser(form) and passing form object in it. But this will not be a regular function, it is a special redux function called the action creator so to be able to call such a function we need to use dispatch from react redux.
action creator code
constants are imported from the contants file.
backend api endpoint code already written to save time
i exported this function to be used in routes file and i have imported the routes file into the main server.js file.
Lets do a test,
There you go. smsbing is a real project and i will post to youtube if you want … subscribe..