After submitting the form, I want to remain on the current page and show a message indicating whether the submission was successful or not. The HTTP POST
request is processed like this:
app.post('/insertdb', function(request, response) {
// insert values from form into database
response.json({success: true});
});
Then, in the client-side .ejs
file, I attempted to use Ajax to display an alert message (following guidance from how to use NodeJS pop up an alert window in browser) as shown below:
<script>
$.ajax({
url: "/dbinsert",
type: "post",
data: 'testing',
cache: false,
success: function(response) {
if(data['success']) {
alert("success");
}
},
error: function () {
alert("There has been an error");
}
});
</script>
The problem arises when clicking the submit button as it navigates away from the page and shows: `{"success":true}` instead of staying on the same page post-form submission with a popup displaying the status of the request.