Next.js Discord

Discord Forum

How to get data from the frontend in my server side page?

Answered
British Shorthair posted this in #help-forum
Open in Discord
Avatar
British ShorthairOP
Hi, I'm new to NextJS! πŸ‘‹

I want to do a login form and therefore made a client component called LoginForm with a html form in it and two inputs. Now, I want to pass the form data onSubmit into the parents ssr-page. The LoginForm Client Component is located in the LoginPage, which is a server side rendered component. There I'm also handling my database operations with mysql. Now, I want to get the data from the client component as soon as the data gets submitted by the user and then work with it in the LoginPage server side component.

What's the easiest way for this?
Thanks in advance!
Answered by B33fb0n3
the jsx inside your LoginPage is for rendering content. You can use an api route, to handle the inputs of your user. After that your flow looks like this:

User clicks submit button -> form onSubmit function will be called -> the function calls fetch to your api endpoint -> your api endpoint handles the request -> returns data if needed -> client (onSubmit function) handles the result -> done.
View full answer

7 Replies

Avatar
@British Shorthair Hi, I'm new to NextJS! πŸ‘‹ I want to do a login form and therefore made a client component called LoginForm with a html form in it and two inputs. Now, I want to pass the form data onSubmit into the parents ssr-page. The LoginForm Client Component is located in the LoginPage, which is a server side rendered component. There I'm also handling my database operations with mysql. Now, I want to get the data from the client component as soon as the data gets submitted by the user and then work with it in the LoginPage server side component. What's the easiest way for this? Thanks in advance!
Avatar
the jsx inside your LoginPage is for rendering content. You can use an api route, to handle the inputs of your user. After that your flow looks like this:

User clicks submit button -> form onSubmit function will be called -> the function calls fetch to your api endpoint -> your api endpoint handles the request -> returns data if needed -> client (onSubmit function) handles the result -> done.
Answer
Avatar
British ShorthairOP
Thank you, I'll give that a shot! :)
Avatar
British ShorthairOP
Thank you so much, I'll try that! :)
Avatar
sure thing