Jump to content

onsubmit synchronous ajax validation


LLLLLLL

Recommended Posts

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;
}

Link to comment
https://forums.phpfreaks.com/topic/257312-onsubmit-synchronous-ajax-validation/
Share on other sites

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.