I´m doing a web application, its a simple form in HTML, there is a button that calls a javascript function to validate the data in the form, but I need call a Servlet if the data is correct.
I have something like this..
<form method="get" id="form" action="ServletForm">
.
.
.
<input type="submit" value="send" onclick="valiData()">
</form>
the info is validated correctly , but I need to know how can I call the servlet if the data is correct to insert in a database.
The fact that you have a type=”submit” button means it should automatically submit the form (to the URL specified by the form’s “action”) unless you deliberately cancel submission.
Use the
onsubmitevent on the form to run your validation. If you return true the submit will proceed, and if you return false it will be cancelled.You should use “post” rather than “get” if the submission updates data.
Try something like this:
Note that
onsubmit="return valiData();"is the key part to cancel/allow the submission to continue.You can also do something similar to cancel/allow the click on the button, but that won’t cover you if the form gets submitted in some other way, e.g., by pressing Enter within a text field.