Create a new data in database without form (POST API)
export default function ButtonText() {
const [workername, setWorkername] = useState();
const [workertype, setWorkertype] = useState();
const [open, setOpen] = useState(false);
const handleButtonClick = async () => {
console.log(workername, workertype);
// or
const data = {
workername,
workertype
};
try {
const response = await fetch("http://surevih.in/api/workerprofile", {
method: "POST",
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ "workername": workername, "workertype": workertype }),
// or
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const result = await response.json();
console.log('Product created successfully:', result);
} catch (error) {
console.error('Error creating product:', error.message);
}
};
const handleClose = () => {
setOpen(false);
};
return (
<>
<Box display='flex' xs={12} paddingBottom={2} >
<Typography paddingLeft={3} variant='h4' >Create Account without Form</Typography>
</Box>
<TextField
required
label="Worker Name"
id='workername'
defaultValue=''
variant="outlined"
margin="normal"
fullWidth
type='text'
value={workername}
onChange={(e) => setWorkername(e.target.value)}
/>
<TextField
required
label="Worker type"
id='workertype'
defaultValue=''
variant="outlined"
margin="normal"
fullWidth
// type='number'
value={workertype}
onChange={(e) => setWorkertype(e.target.value)}
/>
{/* <Subtitletext name={'User Type'} color="true" /> */}
<Button fullWidth={true} style={{ color: "black", backgroundColor: 'pink' }}
type="submit"
variant="contained"
margin="normal"
onClick={handleButtonClick}
>
Create Account
</Button>
</>
);
}
No comments:
Post a Comment