Home > Software design >  Submit form and POST request
Submit form and POST request

Time:08-14

I'm new to React and JavaScript. I want to submit a form based on React and JavaScript:

      <form action='#' method="post" className='mt-4 register-form'>
        <div className='row'>
          <div className='col-sm-6'>
            <div className='input-group mb-3'>
              <input
                type='text'
                className='form-control'
                placeholder='Name'
                aria-label='name'
              />
            </div>
          </div>
          <div className='col-sm-6 '>
            <div className='input-group mb-3'>
              <input
                type='email'
                className='form-control'
                placeholder='Email'
                aria-label='email'
              />
            </div>
          </div>
           
          <div className='col-12'>
            <button
              type='submit'
              className='btn btn-primary mt-4 d-block w-100'
            >
              Get Started
            </button>
          </div>
        </div>
      </form>

Service:

let Contact = {
    name,
    email,
};


import axios from "axios";
import React from "react";

const baseURL = "https://jsonplaceholder.typicode.com/posts";

export default function SubmitContact(Contact) {

    const [post, setPost] = React.useState(null);

    function createPost(Contact) {
        axios
            .post(baseURL, Contact)
            .then((response) => {
                setPost(response.data);
            }).catch(...implement erorr messge here ...);
    }
}

Can you guide me what is the proper way to implement this code?

CodePudding user response:

Edited with react-hook-form

App.js

import { useState } from "react";
import { submitContact } from "./services/sendData";
import { useForm, Controller } from "react-hook-form"; // library

export default function App() {
  const [post, setPost] = useState([]); // I think the data is array

  // REACT-HOOK-FORM
  const { control, handleSubmit } = useForm();

  const onSubmit = (json) => {
    // react-hook-form return a json
    try {
      submitContact(json).then((res) => {
        setPost(res.data);
      });
    } catch (e) {
      console.error(e);
    }
  };
  return (
    <div className="App">
      <form onSubmit={handleSubmit(onSubmit)} className="mt-4 register-form">
        <div className="row">
          <div className="col-sm-6">
            <div className="input-group mb-3">
              <Controller
                control={control}
                name={"name"} //  the key of json
                defaultValue={""}
                render={({ field }) => (
                  <input
                    {...field}
                    type="text"
                    className="form-control"
                    placeholder="Name"
                    aria-label="name"
                    onChange={(e) => field.onChange(e.target.value)} // the value
                  />
                )}
              />
            </div>
          </div>
          <div className="col-sm-6 ">
            <div className="input-group mb-3">
              <Controller
                control={control}
                name={"email"} //  the key of json
                defaultValue={""}
                render={({ field }) => (
                  <input
                    {...field}
                    type="text"
                    className="form-control"
                    placeholder="Email"
                    aria-label="name"
                    onChange={(e) => field.onChange(e.target.value)} // the value
                  />
                )}
              />
            </div>
          </div>

          <div className="col-12">
            <button
              type="submit"
              className="btn btn-primary mt-4 d-block w-100"
            >
              Get Started
            </button>
          </div>
        </div>
      </form>
    </div>
  );
}

Services folder

const baseURL = "https://jsonplaceholder.typicode.com/posts";

export const SubmitContact = (json) => {
  return axios.post(baseURL, json);
};
  • Related