1

I am working on Reactjs(Nextjs),Trying to submit form but unable to submit form,How can i submit form data ? Here is my current code

    export default function Home() {
      const checklogin = async (e:any) => {
        e.preventDefault();
        alert("Its working");
      }
        <form className="forms-sample" onSubmit={checklogin}>
              <div className="form-group first">
                <label htmlFor="username">Username</label>
                <input
                  type="text"
                  className="form-control"
                  placeholder="[email protected]"
                  id="username"
                />
              </div>
              <div className="form-group last mb-3">
                <label htmlFor="password">Password</label>
                <input
                  type="password"
                  className="form-control"
                  placeholder="Your Password"
                  id="password"
                />
              </div>
              <input
                type="submit"
                name="submit"
                defaultValue="Log In"
                className="btn btn-block btn-primary"
              />
            </form>
1
  • What happens with this code ? The alert doesn't show up ? Could you go in the console and see if there's any error ? Commented Feb 4, 2023 at 5:40

1 Answer 1

1

1. controlled form

import React from 'react';

export default function Home() {
    const INITIAL_DATA = {
        username: '',
        password: '',
    };
    const [formData, setFormData] = React.useState(INITIAL_DATA);

    const checklogin = async (e) => {
        e.preventDefault();
        console.log(formData); // * PROCESS FORMDATA ON SUBMIT
        alert('Its working');
        setFormData(INITIAL_DATA); // * CLEAR DATA AFTER SUBMIT
    };

    function handleOnChangeInput(event) {
        const name = event?.target?.name;
        const value = event?.target?.value;
        setFormData((prev) => ({
            ...prev,
            [name]: value,
        }));
    }

    return (
        <form className='forms-sample' onSubmit={checklogin}>
            <div className='form-group first'>
                <label htmlFor='username'>Username</label>
                <input
                    type='text'
                    className='form-control'
                    placeholder='[email protected]'
                    id='username'
                    onChange={handleOnChangeInput}
                    value={formData?.username}
                />
            </div>
            <div className='form-group last mb-3'>
                <label htmlFor='password'>Password</label>
                <input
                    type='password'
                    className='form-control'
                    placeholder='Your Password'
                    id='password'
                    onChange={handleOnChangeInput}
                    value={formData?.password}
                />
            </div>
            <input
                type='submit'
                name='submit'
                defaultValue='Log In'
                className='btn btn-block btn-primary'
            />
        </form>
    );
}

2. Uncontrolled form

import React from 'react';

export default function Home() {
    const formRef = React.useRef(null);

    const checklogin = async (event) => {
        event.preventDefault();
        alert('Its working');

        // * Get form enteries
        const formData = new FormData(formRef?.current);
        const formEnteries = Object.fromEntries(formData?.entries());
        console.log(formEnteries); // * PROCESS FORMENTERIES ON SUBMIT

        // * Clear form fields here
        const formCurrentTarget = event?.currentTarget;
        formCurrentTarget?.reset();
    };

    return (
        <form className='forms-sample' onSubmit={checklogin} ref={formRef}>
            <div className='form-group first'>
                <label htmlFor='username'>Username</label>
                <input
                    type='text'
                    className='form-control'
                    placeholder='[email protected]'
                    id='username'
                    name='username'
                />
            </div>
            <div className='form-group last mb-3'>
                <label htmlFor='password'>Password</label>
                <input
                    type='password'
                    className='form-control'
                    placeholder='Your Password'
                    id='password'
                    name='password'
                />
            </div>
            <input
                type='submit'
                name='submit'
                defaultValue='Log In'
                className='btn btn-block btn-primary'
            />
        </form>
    );
}

Read more about controlled and uncontrolled components here - Link

Sign up to request clarification or add additional context in comments.

1 Comment

Use Formik library for better and easy Forms in React

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.