0

I am having an issue when I submit my form it puts all of the form data into my URL instead of sending it to my backend. I'm not sure what the issue is at first I thought it was because I didn't have a method="post" in the form tag but that didn't fix my issue because it tried to send the form data to localhost:3000/register instead of localhost:5000/register. Any help would be appreciated.

Bellow is my current Frontend code.

import { BrowserRouter as Router, Route, Routes, Link } from 'react-router-dom'
import '../css/register.css';
import {IoMdArrowRoundBack} from 'react-icons/io'
import { useState } from 'react'
import axios, { Axios } from 'axios';


const Register = () => {

    const [emailReg, setEmailReg] = useState("");
    const [usernameReg, setUsernameReg] = useState("");
    const [passwordReg, setPasswordReg] = useState("");

    const register = () => {
        Axios.post('http://localhost:5000/register', {
            email: emailReg,
            username: usernameReg, 
            password: passwordReg, 
        }).catch(function (error) {
            console.log(error);
        });
    };

    return (
        <div className='background-image'>

            <div className='back-button'>
                <Link to='/'>
                    <IoMdArrowRoundBack id='back-arrow' />
                    <h3>Home</h3>
                </Link>
            </div>

        <div className="container-wrapper">
            <div className="container">

            <h1>Create Account</h1>

                <div className="wrapper">
                    <form>
                        <div className="textarea" id="email">
                         <input 
                            type="email" 
                            onChange={(e) => {
                                setEmailReg(e.target.value);
                            }} 
                            name="email" 
                            id="authentactor-email" 
                            placeholder="Email" 
                            defaultValue="" 
                            required 
                          />
                        </div>

                        <div className="textarea" id="username">
                          <input 
                            type="text" 
                            onChange={(e) => {
                                setUsernameReg(e.target.value);
                            }}
                            name="name" 
                            id="authentactor-text" 
                            placeholder="Username" 
                            defaultValue="" 
                            required 
                          />
                        </div>

                        <div className="textarea" id="password">
                          <input 
                            type="password" 
                            onChange={(e) => {
                                setPasswordReg(e.target.value);
                            }}
                            name="password" 
                            id="authentactor-password" 
                            placeholder="Password" 
                            defaultValue="" 
                            required 
                          />
                        </div>

                        <div id="button-wrapper">
                            <button id="button" onClick={register}>Create Account</button>
                        </div>
                    </form>


                    <div className='bottom-text-wrapper'>
                        <h4>Already have an account?   <Link to='/login'>Login Here</Link></h4>
                    </div>
                    
                </div>
            </div>
        </div>
    </div>
    )
}

export default Register
Alex
  • 238
  • 4
  • 14
  • I'm not super familiar with `axios`, but shouldn't you be using the `axios` function instead what I imagine is the type `Axios`? I.e. this: `Axios.post('http://localhost:5000/register', {` Should be: `axios.post('http://localhost:5000/register', {` – bopbopbop Jan 25 '22 at 20:16
  • So sounds like you are not cancelling the form submission. Submit buttons submit forms. – epascarello Jan 25 '22 at 20:17
  • @epascarello the button does not have the "type='submit'" value so it shouldn't be submitting the form the regular way, it should be executing the register function instead. – Alex Jan 25 '22 at 20:21
  • @bopbopbop it doesn't make a difference rather I have Axios.post or axios.post I get the same error. – Alex Jan 25 '22 at 20:22
  • Can you try adding preventDefault on the event handler. – hazimdikenli Jan 25 '22 at 20:36
  • 2
    @AlexanderMurdock If you do not provide a type to ` – epascarello Jan 25 '22 at 21:41

2 Answers2

4

According to HTML Living Standard

The missing value default and invalid value default are the Submit Button state.

You can find more information on this question but basically adding type="button" to your Create Account button should do the job.

(so something like <button id="button" type="button" onClick={register}>Create Account</button>)

Berci
  • 2,876
  • 1
  • 18
  • 28
  • I tried this it didn't work I get a POST http://localhost:5000/register 409 (Conflict) – Alex Jan 25 '22 at 20:34
  • 2
    Sounds like it IS working and your backend is throwing an error. Are you creating two accounts with the same info, that sounds like a conflict. Look at the server logs. – epascarello Jan 25 '22 at 21:46
-2

I figured it out for some reason I can't have the "name" tag in my input fields like it is below.

  <div className="textarea" id="password">
      <input 
        type="password" 
        onChange={(e) => {
            setPasswordReg(e.target.value);
        }}
        name="password" 
        id="authentactor-password" 
        placeholder="Password" 
        defaultValue="" 
        required 
      />
    </div>

<div id="button-wrapper">
    <button onClick={register} id="button">Create Account</button>
</div>

As soon as I removed the "name" tags I was able to POST my form to the backend and I only get a question mark in my url now instead of all the form data. To fix the question mark I had to set button type="button".

correct code below.

  <div className="textarea" id="password">
      <input 
        type="password" 
        onChange={(e) => {
            setPasswordReg(e.target.value);
        }} 
        id="authentactor-password" 
        placeholder="Password" 
        defaultValue="" 
        required 
      />
    </div>

<div id="button-wrapper">
    <button type="button" onClick={register} id="button">Create Account</button>
</div>
Alex
  • 238
  • 4
  • 14
  • I think you are still not posting:) – hazimdikenli Jan 25 '22 at 20:36
  • it is I am getting the user information posted to my database now where before it wasn't making it to the database. This is the solution. – Alex Jan 25 '22 at 20:37
  • I think you are now successfully making a get request, still not post. otherwise you would not be getting them in the url, you would be getting them in the form data. So you fixed an issue, but not the one that was preventing the post request. You can add some console logging to check this behaviour. – hazimdikenli Jan 25 '22 at 20:41
  • It's Posting. If it was making a GET request the data from my form would not be ending up in my database. GET retrieves data while POST sends data. So if it was making a GET request I would still be having the same issue. – Alex Jan 25 '22 at 20:46
  • You still have a bug... you are still submitting the form. – epascarello Jan 25 '22 at 21:48
  • This is not the issue – SamiElk Jan 26 '22 at 00:22
  • 2
    I am just trying to tell you that `it is working but not the way it was intended by you`, if you are okay with the way it is, no problema for me. (as we are not working on the same code). – hazimdikenli Jan 26 '22 at 06:44
  • You only get a question mark in your url because you basically submit an empty form. You can read more about forms here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form . But you are currently misusing the form and the attributes – Berci Jan 26 '22 at 08:18
  • To get all of the form data to POST and to get rid of the question mark in the URL I had to remove the name tag and also add – Alex Jan 27 '22 at 21:48