I'm trying to register a user to my database but I can't even make my post request work. The get part router is working but it seems post is not working.
In the app I declared routers like this(server.js file):
const userRoutes = require("./routes/user.js")
app.use('/user',userRoutes)
In the routes/user.js file:
//User register
const userController = require('../controllers/user_controller')
router.get("/register",userController.register_page)
router.post("/register",userController.register_post)
And this is the controller part (controllers/user_controller.js):
//Register get
exports.register_page = (req,res)=>{
return res.render('user/register',{layout:false})
}
//Register post
exports.register_post = async (req,res)=>{
//bla bla
}
I can see the pages but when I submit in the ejs page nothing happens. This is the view page(views/user/register.ejs):
<form id="register" method="post" action="/user/register">
<label><b>Email
</b>
</label>
<input type="text" name="email" id="email" placeholder="Email">
<br><br>
<label><b>Password
</b>
</label>
<input type="Password" name="password" id="password" placeholder="Password">
<br><br>
<label><b>Confirm Password
</b>
</label>
<input type="Password" name="confirm" id="confirm" placeholder="Password Repeat">
<br><br>
<input type="button" name="submit" id="submit" value="Register">
</form>
Your form does not send the post request, because the submit button does not have the type of submit. Change from type="button" to type="submit".
Related
I'm trying to send a POST request to an API from multiple forms at once. Essentially I have a site where users will input data to multiple locations before clicking a button to run a calculation. Clicking the button will ideally send a POST request to the API containing all the information the user has input from various places.
Within the body of index.html I've tried the following, effectively trying to get both forms to POST to the same address, but only including a submit button with one.
<form action="/" method="post" name="myForm">
<div>
Here is some text at the top of the form...
</div>
<input type="text" name="num1" class="inp1" placeholder="First Number">
<input type="text" name="num2" class="inp2" placeholder="Second Number">
<button type="submit" name="submit">
calculator
</button>
</form>
<form action="/" method="post" name="myForm">
<div>
Here is a 2nd form
</div>
<input type="text" name="num1" class="inp1" placeholder="Third Number">
<input type="text" name="num2" class="inp2" placeholder="Fourth Number">
</form>
My Node.js code to spin up a server and look at the POST request:
const fs = require("fs");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(
bodyParser.urlencoded({
extended: true,
})
);
const tempResult = fs.readFileSync(
`${__dirname}/templates/overview.html`,
"utf-8"
);
app.get("/", (req, res) => {
res.send(tempResult);
});
app.post("/", (req, res) => {
console.log(req.body);
res.send(tempResult);
});
const port = 3000;
app.listen(port, () => {
console.log(`App running on port ${port}...`);
});
In the console this simply returns:
{
num1: val1,
num2: val2,
submit: ''
}
Where val1 and val2 are the inputs to index.html for the first form only. What am I missing here? How can I get the values from both forms in one call?
As alluded to by #Ipizzinidev the solution here lies in putting the form right at the top of the body.
<body>
<form action="/" method="POST">
<div>
Here is some text at the top of the form...
</div>
<input type="text" name="num1" class="inp1" placeholder="First Number">
<input type="text" name="num2" class="inp2" placeholder="Second Number">
<input type="text" name="num3" class="inp3" placeholder="Third Number">
<input type="text" name="num4" class="inp4" placeholder="Fourth Number">
<button type="submit" name="submit">
calculator
</button>
</form>
</body>
Inside this form you can do the usual HTML stuff and then after hitting submit it will return the following:
{
num1: val1,
num2: val2,
num3: val3,
num4: val4,
submit: ''
}
It always seems so simple in hindsight!
I have this weird issue and it seems that I can't resolve it.
Whenever I submit my form, it goes to a "double route".
This is my html form.
<form action="eveniment/<%=eveniment._id%>" method="POST" id="modificaeveniment">
<input type="text" placeholder="titlu eveniment" name="titlu" value="<%= eveniment.name %>">
<button class="btn btn-info">Submit</button>
</form>
This is the route:
app.post("/eveniment/:id", function(req, res){ res.send("post route"); }
And I always get the error:
Cannot POST /eveniment/5f1740204a5a2206cc02b5af/eveniment/5f1740204a5a2206cc02b5af
It looks like somehow it doubles the route.
Just add a / at action="eveniment/<%=eveniment._id%>"
<form action="/eveniment/<%=eveniment._id%>" method="POST" id="modificaeveniment">
<input type="text" placeholder="titlu eveniment" name="titlu" value="<%= eveniment.name %>">
<button class="btn btn-info">Submit</button>
</form>```
I am using ejs template, expressjs, and mongoose. not able to update existed data through form while click on edit button and as well not able to delete it . I want to delete when user click a button as well when user click on edit button it show form and allow him to edit. I already wrote get route , it is working fine.
**Update route:**
router.put('/success123' , function (req, res) {
// const id = req.params.id;
Campaign.findById(id)
.then(campaign => {
campaign.Title = req.body.title;
campaign.Description = req.body.Description;
campaign.save().then(updatePost => {
res.render('success123');
});
});
});
**Delete route**
router.delete ('/delete/:id' , function (req, res){
Campaign.findByIdAndDelete(req.params.id)
.then(deletedPost => {
res.render('success');
});
});
I am getting error and cant figure it out . Event it is not showing any error message in my console. both deleting and updating parts not working and i am able to success fully get route while user click on edit campaign button.
My ejs template for update : THIS IS MY EJS TEMPALTE WHERE I AM SENDING UPDATE INFORMATION THROUGH FORM
<div class="row p-4">
<div class="col-md-7">
<form action="/success123" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Title</label>
<input type="text" value="<%=camplist.Title%>" class="form-control" name="title" id="title" placeholder="Enter The Title">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea value="" name="description" id="description" class="form-control" placeholder="Enter your content here" rows="10"><%=camplist.Description%></textarea>
</div>
<div>
<input class="form-control" name="rules" type="hidden" placeholder="Enter The Title">
<textarea name="rules" id="editor"></textarea>
</textarea>
</div>
<div class="form-group">
<label for="file">Upload Your Banner Image </label>
<input type="file" class="form-control" id="file" name="uploadedFile" accept="image/jpeg, image/jpg, image/png, image/bmp">
</div>
<button class="btn btn-outline-success btn-lg" type="submit">Update Post</button>
</form>
I am getting error and cant figure it out . Event it is not showing any error message in my console. both deleting and updating parts not working and i am able to success fully get route while user click on edit campaign button.
i have using ejs engine to get and post method.When i use http://localhost:8080/signup, i will get a sigup form where i can input my values. The problem is after submitting the form,i am unable console the value of "req.body". Help me?
app.post('/signup',urlencodedParser,function(req,res){
//console.log(req)
var names = req.body;
console.log(names);
res.end("sigup submitted");
});
app.get('/signup', function(req, res) {///////////signup ejs loading
res.render('signup')
});
<form id="signupForm" enctype="multipart/form-data" method="post" action="">
<div class="container">
<h1>Sign Up</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<label for="name"><b>Company Name</b></label>
<input type="text" placeholder="Company Name" name="name" required>
<label for="email"><b>Email</b></label>
<input type="text" placeholder="Enter Email" name="email" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<label for="psw-repeat"><b>Repeat Password</b></label>
<input type="password" placeholder="Repeat Password" name="psw-repeat" required>
<label for="psw-repeat"><b>Address</b></label>
<input type="text" placeholder="Address" name="address" required>
<label>
<input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me
</label>
<p>By creating an account you agree to our Terms & Privacy.</p>
<div class="clearfix">
<button type="button" class="cancelbtn">Cancel</button>
<button type="submit" class="signupbtn">Sign Up</button>
</div>
</div>
</form>
Express does come with some body parsers, but if you are using multipart/form-data then answer would be https://github.com/expressjs/multer not
You can even read that https://www.npmjs.com/package/body-parser (aka body-parser) also do not read multipart/form-data as they can be complicated and should be used only when files are sent.
hello guys i m new to loopback, don't know to post data to model eg:
here is my login.ejs file
<form action="/login" method="post">
<input type="text" name="username" placeholder="username">
<input type="password" name="password" placeholder="password">
<input type="submit" name="" value="Submit">
</form>
now when user click on login how i can submit send it to model through router of directly call model root.js. i m i missing something
app.post('/login', login.index);
how i can call login model from root.js or directly access login model js