React POST FORM
import React, { useState } from "react";
import axios from 'axios';
function LatestForm(){
const [city,setCity] = useState("");
const [state,setState] = useState("");
const [country, setCountry] = useState("");
const handleSubmit=(event)=>{
event.preventDefault();
axios.post(`http://127.0.0.1:8000/`,{city,state,country})
.then(response=>{
alert("data has been registered on database.")
})
.catch(error=>{
console.error(error);
});
};
return (
<>
<form method="POST" onSubmit={handleSubmit}>
<input type="text" placeholder='city' value={city} onChange={e=>setCity(e.target.value)}/><br/>
<input type="text" placeholder='state' value={state} onChange={e=>setState(e.target.value)} /><br/>
<input type="text" placeholder='country' value={country} onChange={e=>setCountry(e.target.value)} />
<button type="submit">Submit</button>
</form>
</>
)
};
export default LatestForm;
Comments
Post a Comment