admin管理员组

文章数量:1287858

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!

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!

Share Improve this question asked Aug 26, 2013 at 7:57 vladzamvladzam 5,9186 gold badges33 silver badges36 bronze badges 2
  • you just could send the form via ajax, send a JSON response and display the alert based on the data. Or, since you're using node, you might want take a look at socket.io ... – gherkins Commented Aug 26, 2013 at 8:02
  • 1 Send the variables via ajax (jQuery) and return a res.json(true) and in the SUCCESS function pop the alert? – vladzam Commented Aug 26, 2013 at 8:04
Add a ment  | 

1 Answer 1

Reset to default 8

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});
});

本文标签: javascriptExpressjsSend an alert as a response while staying on same pageStack Overflow