<script>
var xmlhttp;
if(window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("POST", "sign_up(1).php", true);
xmlhttp.send(); // Send the request
$('form').on('submit', function (e) {
e.preventDefault(); // Block default commit behavior
// How to let the user submit the form if all the input in valid?
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
// Checking the error messages returned by PHP
if (data.error_msg_1 !== "" || data.error_msg_2 !== "" || data.error_msg_3 !== "" || data.error_msg_4 !== "") {
$('input[type="submit"]').prop('disabled', true); // Disable the SUBMIT button
}
else{
// Submit the form
document.getElementById("signup-form").submit();
}
}
};
});
</script>
'Why is there no error message returned and the form is still not submitted? I want to return an error message if the user makes a typo, or vice versa, and have them submit the form. If I submit the form normally, it works fine, so the php/mysql part is not the problem. But after I added preventDefault(), I can't submit the form even without any error message. I don't know what the problem is. Is the submit() function not behaving properly? My purpose is that when the user submits the form, the system will first submit the form to the background for processing, then perform verification, and then send the information returned after verification to the foreground. If the information is an error message, the user will be prevented from submitting the form. Instead, let the user submit the form. (I want these actions to all be done without refreshing the page)'