I have a simple question that I haven't been able to find a satisfactory solution for. I've created a post route using express, like this:
app.post('/search', function(req, res){
// Code to extract data from req and save it to the database
// Want to stay on the current page afterwards.
});
Here's my html form:
<form action="/search" method="post">
<input value="name" name="marker[name]">
<input value="address" name="marker[address]">
<input value="rating" name="marker[rating]">
<button id="favo-button" type="submit">
Submit
</button>
</form>
When the user clicks the submit button, the data is sent through req to the post route and inserted into the database. However, I want the page to remain on the current page. I've tried a few approaches but none have worked well. Here's what I've attempted:
- In the route,
res.render('/search');
This causes the page to re-render with the same view, but there's a flashing effect due to the re-render.
- Not doing anything with res in the route results in the page loading circle never stopping.
app.post('/search', function(req, res){
var data = req.body.marker;
// Some code to insert data into the database
// Then do nothing with res. The page stays but keeps loading.
});
Is there a way to simply do nothing with res
and stay on the page without the loading issue?