Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.