I want to submit the data form with a submit button so the database will be updated with the supplied values. Its a nodeJS project and front end is a jade file
Jade file
html
head
body
form(method='post' action='/saveEdit/#{rows[0].id}')
div.form-group
label(for="id") Id:
input#id(type='text' name='id' value= "#{rows[0].id}" )
div.form-group
label(for="mname") Name:
input#name(type='text' name='mname' value= "#{rows[0].name}" )
div.form-group
label(for="mage") Age:
input#age(type='text' name='mage' value= "#{rows[0].age}" )
div.form-group
label(for="mcity") City:
input#city(type='text' name='mcity' value= "#{rows[0].city}" )
div.form-group
label(for="mdepartment") Department:
input#department(type='text' name='mdepartment' value= "#{rows[0].department}" )
div.form-group
button(type='submit') Save
my app.js as follows
myapp.js
app.post('/saveEdit/:id',function(req,res){
var id = req.params.id;
var Query = 'UPDATE test SET id = ?, name = ? , age=? , city= ? , department =? WHERE id= ?';
//Check this log in console, it should print all the values properly, if not issue is at clinet side
console.log('Input:: name='+req.body.mname+' age='+ req.body.mage +' city='+ req.body.mcity + ' dep=' + req.body.mdepartment, req.body);
connection.query(Query,[req.body.id,req.body.mname,req.body.mage,req.body.mcity,req.body.mdepartment],function(err,res){
if(err) {
console.error('Error in sql query:' + Query + ' Err', err);
throw err;
}
});
res.redirect('/');
});
via Tharaka_Ravishan
No comments:
Post a Comment