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 https://forums.phpfreaks.com/topic/105940-ajax-form-validation/ Share on other sites More sharing options...
stylusrose Posted May 16, 2008 Share Posted May 16, 2008 first of I do believe you're on the wrong board, second off: http://www.yourhtmlsource.com/javascript/formvalidation.html answer Quote Link to comment https://forums.phpfreaks.com/topic/105940-ajax-form-validation/#findComment-542910 Share on other sites More sharing options...
BlueSkyIS Posted May 16, 2008 Share Posted May 16, 2008 you're best off using validation in PHP on the server, in addition to or instead of client-side validation. anyone can turn off Javascript and bypass Javascript validation (and nuke ajax as well). If you don't also validate on the server, you leave yourself open to invalid submissions. imo, ajax is good for data-intensive operations where there are many values to choose from and sending them all to the browser at once wouldn't work well. for instance, ajax is a good choice to provide a list of possible zip codes for a selected state. you don't want to list every single US zip code for the user to pick from, so you could use ajax to reduce the selection based on the user's state selection. but, if you also want to validate client-side, add an onsubmit() handler to the form, returning true (will submit) or false (won't submit) from a javascript validation function. http://www.w3schools.com/jsref/jsref_onsubmit.asp Quote Link to comment https://forums.phpfreaks.com/topic/105940-ajax-form-validation/#findComment-542914 Share on other sites More sharing options...
RichardRotterdam Posted May 16, 2008 Share Posted May 16, 2008 plz dont crosspost http://www.phpfreaks.com/forums/index.php/topic,197481.0.html Quote Link to comment https://forums.phpfreaks.com/topic/105940-ajax-form-validation/#findComment-543002 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.