ksmatthews Posted May 19, 2008 Share Posted May 19, 2008 HI All, I understand how ajax can be used to enhance the user experience in providing clues about invalid user input into forms. What I would like to know is whether or not there is a way of using ajax to actually PREVENT a form being submitted unless all fields have been correctly completed. It is certainly possible to disable the submit button when a field has been incorrectly filled in, but how could you do this for multiple fields ? Here are some code snipptes ... .......... .......... // assemble URL string var url = "../global_php/ajax_validation.php"; url = url + "?value=" + str.value + validation_fields; url = url + "&sid=" + Math.random(); // The onreadystatechange property stores the function that will process the response from the server // Each time the readyState changes, the onreadystatechange function will be executed. xmlHttp.onreadystatechange = stateChanged; xmlHttp.open("GET", url, true); xmlHttp.send(null); function stateChanged() { // The readyState property holds the status of the server's response if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { // capture result var response = xmlHttp.responseText; // The data sent back from the server is assigned to submitted destination document.getElementById(destination_id).innerHTML = response; // set submit button if(response.indexOf("Error") == -1) document.getElementById("submit").disabled = false; else document.getElementById("submit").disabled = true; } } regards, Steven M Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.