why i cant get data from html form using post method? - node.js

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.

Related

Undefined when trying to get data from form

Same code works fine with other application but for some reason won't work in this one
HTML:
<form action="/employees/add" method="POST" enctype="multipart/form-data" >
<div class="form-group">
<label for="first_name">First Name</label>
<input name="first_name" type="text" class="form-control" id="first_name" value="first_name"placeholder="Please enter your first name">
</div>
<div class="form-group">
<label for="last_name">Last Name</label>
<input name="last_name" type="text" class="form-control" id="last_name" value="last_name" placeholder="Please enter your last name">
</div>
</form>
Server.js
app.post('/employees/add', (req, res) => {
data.addEmployee(req.body).then(() =>{
console.log(req.body);
res.redirect('/employees')
});
Output:
Server running 8080
undefined
Of course, when I call addEmployee since req.body is undefined it won't do anything
I tried different app.use(express.static... ) but no luck and here how my folder looks like

how can i change string format of Price in database to integer format in mongodb?

This is my input form. Here, the Price label save as string in Database. How can change it into integer format. I'm using mongodb 3.2 version
<section>
<div class="container mt-4">
<div class="row">
<div class="col-md-6">
<h2 class="text-center">Add Product</h2>
<form action="/admin/add-product" method="POST" enctype="multipart/form-data">
<label for="">Name</label>
<input type="text" name="Name" class="form-control">
<label for="">Category</label>
<input type="text" name="Category" class="form-control">
<label for="">Price</label>
<input type="number" name="Price" class="form-control">
<label for="">Description</label>
<input type="text" name="Description" class="form-control">
<label for="">Image</label>
<input type="file" name="Image" class="form-control">
<button type="submit" class="btn btn-success mt-4">Submit</button>
</form>
</div>
</div>
</div>
</section>
Add to cart
addProduct:(product,callback)=>{
db.get().collection("product").insertOne(product).then((data)=>{
console.log(data)
callback(data.ops[0]._id)
})
}
Before adding the product to database change Price to integer
let Price=parseInt(req.body.Price)
req.body.Price=Price
then add product to mongodb

NodeJs project how to make user input in a form unsanitized?

I have a form in a NodeJs project where I am using:
app.use(bodyParser.urlencoded({extended: true}));
I want to make it where if a user enters <h1>hello</h1> into the form that it will show up as "hello" formatted as an h1 instead of <h1>hello</h1>
<form action="/blogs" method="POST">
<div class="form-group">
<input class="form-control" type="text" name="name" placeholder="name">
</div>
<div class="form-group">
<input class="form-control" type="text" name="image" placeholder="image url">
</div>
<div class="form-group">
<input class="form-control" type="text" name="description" placeholder="description">
</div>
<div class="form-group">
<button class="btn btn-lg btn-primary btn-block">
Submit
</button>
</div>
</form>
I found a solution to my problem.
In Node.js if you want to display information as HTML, in the index.ejs file
you need to do a
<%- blog.description %>
, where that dash is the key.
Before i had
<%= blog.description %>.

Register new 'etudiant'

Hello so i want to register a new user called 'etudiant' in my mongodb with a post request with
<form method="post" action="register">
<h1>Create Account</h1>
<div>
<input type="text" class="form-control" placeholder="first_name" required="required" name="first_name"/>
</div>
<div>
<input type="text" class="form-control" placeholder="last_name" required="required" name="last_name"/>
</div>
<div>
<input type="text" class="form-control" placeholder="cin" required="required" name="cin"/>
</div>
<div>
<input type="text" class="form-control" placeholder="adress" required="required" name="adress"/>
</div>
<div>
<input type="text" class="form-control" placeholder="date_naissance" required="required" name="date_naissance"/>
</div>
<div>
<input type="email" class="form-control" placeholder="Email" required="required" name="mail"/>
</div>
<div>
<input type="password" class="form-control" placeholder="Password" required="required" name="pwd"/>
</div>
<div style="margin:auto;">
<center><input type="submit" value="Register"></center>
</div>
</form>
and the controller for this form is
function register (req, res) {
//create etudiant
var etudiant{
first_name:req.body.first_name,
last_name:req.body.last_name,
cin:req.body.cin,
adress:req.body.adress,
username:req.body.username,
email:req.body.email,
pwd:req.body.pwd
}
//use etudiant model to insert/save
var newetudiant = new Etudiant(etudiant);
//save etudiant
newetudiant.save();
//redirect
res.redirect('/');
}
in the console i got this error that i don't find a solution for
my error
i'm sorry i was working 9hours straight i didn't see my error xD
problem solved still if you look at the
newetudiant.save();
it's right but still doesn't save data to the db

how to remove html and javascript from input field before sending to server

<form action="process_reg.php" method="post" name="register_form" id="register_form">
<input class="logbar" id="fname" name="fname" type="text" placeholder="Firstname" onfocus="CheckFname(); return true;" onblur="leaveFname();" required>
<span id="fnameMessage"></span>
<input class="logbar" id="lname" name="lname" type="text" placeholder="Lastname" onfocus="CheckLname(); return true;" onblur="leaveLname();" required>
<span id="lnameMessage"></span>
<input class="logbar" id="email" name="email" type="text" placeholder="Email" onfocus="CheckEmail();" onblur="leaveEmail();" required>
<span id="emailMessage"></span>
<input class="logbar" id="password" name="password" type="password" placeholder="Password" keyev="true" required >
<input class="logbar" id="password2" name="password2" type="password" placeholder="Confirm Password" onkeyup="checkPass(); return false;" required>
<span id="confirmMessage" class="confirmMessage"></span>
<input value="Logga in" type="button" onclick="formhash(this.form, this.form.password);" id="register">
</form>
and the Sanitize/XSS protection that i applied on this form is given
if (empty($_REQUEST) === false)
{
$regemail1 = filter_input('INPUT_REQUEST', 'email', 'FILTER_SANITIZE_EMAIL');
$regfirst1 = filter_input('INPUT_REQUEST', 'fname', 'FILTER_SANITIZE_SPECIAL_CHARS');
$reglast1 = filter_input('INPUT_REQUEST', 'lname', 'FILTER_SANITIZE_SPECIAL_CHARS');
$regpass = $_POST['p'];
$regemail = htmlspecialchars($regemail1);
$reglast = htmlspecialchars($reglast1);
$regfirst = htmlspecialchars($regfirst1);
$regemail =
}
When I enter Hi it is perfect in a way that it stop the function of tag. but I want to send only text to the database and remove all the other things.
The only value being send to the server in the sample bellow is what ever is input field, you don't send html to server.
<input class="logbar" id="password" name="password" type="password" placeholder="Password" keyev="true" required >
you always check what you are getting in the server by printf, echo

Resources