JavaScript code to stop form submission

You can use the return value of the function to prevent the form submission <form name=”myForm” onsubmit=”return validateMyForm();”> and function like <script type=”text/javascript”> function validateMyForm() { if(check if your conditions are not satisfying) { alert(“validation failed false”); returnToPreviousPage(); return false; } alert(“validations passed”); return true; } </script> In case of Chrome 27.0.1453.116 m if above … Read more

Send POST data using XMLHttpRequest

The code below demonstrates on how to do this. var http = new XMLHttpRequest(); var url=”get_data.php”; var params=”orem=ipsum&name=binny”; http.open(‘POST’, url, true); //Send the proper header information along with the request http.setRequestHeader(‘Content-type’, ‘application/x-www-form-urlencoded’); http.onreadystatechange = function() {//Call a function when the state changes. if(http.readyState == 4 && http.status == 200) { alert(http.responseText); } } http.send(params); In … Read more

jQuery AJAX submit form

This is a simple reference: // this is the id of the form $(“#idForm”).submit(function(e) { e.preventDefault(); // avoid to execute the actual submit of the form. var form = $(this); var actionUrl = form.attr(‘action’); $.ajax({ type: “POST”, url: actionUrl, data: form.serialize(), // serializes the form’s elements. success: function(data) { alert(data); // show response from the … Read more

Servlet returns “HTTP Status 404 The requested resource (/servlet) is not available”

Introduction This can have a lot of causes which are broken down in following sections: Put servlet class in a package Set servlet URL in url-pattern @WebServlet works only on Servlet 3.0 or newer javax.servlet.* doesn’t work anymore in Servlet 5.0 or newer Make sure compiled *.class file is present in built WAR Test the … Read more

My form’s validate() is not working

You should use document.getElementsByName if you want to fetch the value using name attribute. I would recommend to do it by “id” attribute. function submitFunction() { var firstName = document.getElementsByName(“first_Name”)[0].value; if (firstName == “” || firstName == null || firstName.length == 0) { alert(“First Name is required”); return false; } } Also add return to … Read more