Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express.js - Send an alert as a response while staying on same page

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!

like image 869
vladzam Avatar asked Aug 26 '13 07:08

vladzam


1 Answers

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});
});
like image 89
Yalamber Avatar answered Nov 09 '22 01:11

Yalamber