I have 3 forms on a view that, when submitted, add new entries in a mysql database. I would like to send an alert saying "You have successfully added X" whenever an entry is added, without navigating from the page.
// Form to be Submitted
<form method="post" action="route/action/">
<input type="text" name="name">
</form>
// Route
exports.action = function (req, res) {
client.query();
// What kind of response to send?
}
How can I send an alert? What kind of response should I send?
Thank you!
What you will need to do is ajax request to your express server and evaluate the response and alert the user accordingly. This client part you would do same as other programming language.
for example. in jquery client side part you can do this
$.ajax({
url: 'route/action/',
type: "POST",
data: 'your form data',
success: function(response){
alert('evaluate response and show alert');
}
});
In your epxress app you can have something like this
app.post('route/action', function(req, res){
//process request here and do your db queries
//then send response. may be json response
res.json({success: true});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With