I have this node.js application in where I have a very simple feedback form. So that when the user hits the submit button it fires the POST call to the app.js file and this inserts the feedback into the table in DB.
Now, the problem is that when the form is submitted, the page starts reloading forever and appending the form action into the url, such as www.example.com/feedback which obviously is wrong and that path doesn't exist.
Ideally, what I would like to have is when the submit button is pressed the page will not realod maybe similar to ajax?
can anyone help please?
Code below:
app.js
function connect_DB() {
connection;
return connection;
}
app.post("/feedback", function (req, res) {
var obj_connect_DBD = connect_DB();
var post = {feedback: req.body.feedback};
obj_connect_DBD.query('INSERT INTO feedback SET ?', post, function (error) {
if (error) {
console.log(error.message);
} else {
console.log('succes');
}
});
});
form in html file:
<form name="feedback" action="/feedback" method="POST" >
<textarea rows="7" cols="60" id="feedback" name="feedback" required placeholder="Enter feedback..."></textarea>
<br />
<button type="submit" value="submit" id="submit">Send Feedback</button>
</form>
Many thanks
FIXED All I did was, changing the button type to button instead of submit and then I created a function as below:
$("#submit").click(function () {
var feedback = $("#feedback").val();
$.post('/feedback/', {
feedback: feedback
});
});
No page reload anymore :)