main.js
app.post('/count', function(req, res){
var num_ = Object.keys(req.body)[0];
var sql = 'select times from db where num=?'
conn.query(sql, [num_], function(err, rows){
if(err){
console.log(err);
res.status(500).send('Internal Server Error');
} } else{
var times_ = rows[0].times
var sql='update db set times=? where num=?';
conn.query(sql, [times_+1, num_], function(err, rows_){
if(err){
console.log(err);
res.status(500).send('Internal Server Error');
} } else {
}
})
}
})
})
output.jade
form(action='/count' method='post')
input(type='submit', value='+', name=i.num, onclick='window.location.reload()')
If you press the button, the data in the db is modified and the modified data is printed on the page. When you refresh in your browser, the page reloads properly, but when you reload through the click event Ronclick='window.location.reload()'s, the page does not load. What should I do?
node.js express jade
It is not recommended to hand over the submit button by giving the onClick
event or a separate name
attribute. I carefully predict that you should do this.
??.jade
form(action='/count' method='post')
input(type='hidden', name='num', value=i.num)
input(type='submit', value='+')
main.js
app.post('/count', function (req, res) {
var num_ = req.param.num;
var sql = 'select times from db where num=?'
conn.query(sql, [num_], function (err, rows) {
if (err) {
// Run on selection query failure
// Let's skip...
} } else {
// Run Update Query
// Let's skip...
if (err) {
// Run on Update Query Error
// Let's skip...
} } else {
// Redirect here if all is successful
return res.redirect(req.get('referer'))
}
})
}
})
})
© 2024 OneMinuteCode. All rights reserved.