LLLLLLL Posted February 19, 2012 Share Posted February 19, 2012 I'm trying to do a synchronous call for a form's onsubmit function. The server-side code returns "true" or an error message. The problem is that the code that should not be reached (see example below) is actually reached. The "should not be reached" code is only there for this example; without it, the function returns true even though an error message was displayed to the user. So I guess I'm just asking for some help on synchronous ajax form validations. Am I not doing it right? function are_quantities_valid() { $.ajax( "server_file.php", { async: false, data: { sid: i_pass_the_session_id_to_the_server }, success: function ( data ) { if ( data == "true" ) return true; alert( data ); return false; } }); alert( 'this should not be reached.' ); return false; } Quote Link to comment https://forums.phpfreaks.com/topic/257312-onsubmit-synchronous-ajax-validation/ Share on other sites More sharing options...
LLLLLLL Posted February 19, 2012 Author Share Posted February 19, 2012 Update: I changed the code to this, and it works. I guess I need to return the variable outside the success handler, according to some google results I found. function are_quantities_valid() { toReturn = false; $.ajax( "server_file.php", { async: false, data: { sid: the_session_id }, success: function ( data ) { if ( data == "true" ) toReturn = true; else alert( data ); } }); return toReturn; } Quote Link to comment https://forums.phpfreaks.com/topic/257312-onsubmit-synchronous-ajax-validation/#findComment-1318922 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.