Click here to Skip to main content
15,357,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have a code for submitting a form to database and i am using express.js in that when submitting the form it works well but when i click on the refresh page or F5 the for is re-submitted i need to know how can i solve this issue
1-controller method
JavaScript
static create = async(req,res,next)=>{
       try{
           let {firstName,lastname,email,phone,username,password,comments,status} = req.body
           let user = new Users(firstName,lastname,email,phone,username,password,comments,'inactive');
           let [created_user,_] = await user.createUser();
           if(created_user){
               let insertedID = await created_user.insertId
               let [users_info,__] =  await user.addUsersInfo(insertedID)
               res.render('createuser',{title:'Create User', message:`user created with id : ${insertedID}`})
           }
       }catch(err){
           next(err)
       }
   }


2-Form
HTML
<form class="" action="/users/redirict" method="post" >
    <div class="user-insert-rows">
        <div class="form-floating mb-3">
            <input type="text" class="form-control" id="first-name" name="firstName" placeholder="first name">
            <label for="first-name">First Name</label>
          </div>
          <div class="form-floating">
            <input type="text" class="form-control" id="last-name" name="lastname" placeholder="last name">
            <label for="last-name">Last Name</label>
          </div>
    </div>


3-Routers

JavaScript
userRouter.get('/index',newuserController.index)
userRouter.post('/redirict',newuserController.redirict)
userRouter.post('/create',newuserController.create)


What I have tried:

i tried the router by all ways
JavaScript
userRouter.get('/create',newuserController.index)
userRouter.post('/create',newuserController.create)


and at the for action i used
HTML
<form class="" action="/users/create" method="post" ></form>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900