ksmatthews Posted May 16, 2008 Share Posted May 16, 2008 HI All, I would like to ask a question about ajax and form validation I am using some ajax code as follows ... function showHint(str) { xmlHttp = GetXmlHttpObject(); if (xmlHttp == null) { alert ("Browser does not support Ajax HTTP Requests"); return; } // get ID of destination object results = validation_fields.split("|"); destination_id = results[2]; if(str.length==0) { document.getElementById(destination_id).innerHTML=""; return; } // 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: Checks server state and returns data to calling form // PARAMETERS: NONE // RETURNS: void (populates form object with returned data) 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; } } OK. The xmlHttp.responseText will show on the form whatever my php script echoes - in this case error messages for incorrect input. I need to be able to process those ajax generated messages client side in order to prevent my form from being submitted. At the moment the messages are displaying OK but the user can choose to ignore them and submit erroneous data anyway. How can ajax and javascriipt be used to actually enforce client side validation ? I have thought about disabling the submit button but how ?? regards, Steven M Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted May 16, 2008 Share Posted May 16, 2008 always use server side validation as well youre ajax validation should only be used to improve user experience since javascript can simply be turned off. as for ajax validation why use ajax and not just javascript without the ajax. ajax validation is a bit tricky 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.