2 Replies - 752 Views - Last Post: 20 November 2009 - 10:06 PM

#1 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

((Sloved)) function can't return any value

Posted 17 November 2009 - 10:54 PM

Hi guys I am creating a sign up form and validating (Username, Email and Password) with jQuery in side a javascript function but it can't return the exact value.
here is my code
function user(){
	var result = null;
	$("#div_user").removeClass().addClass('checking').text('Checking Username...').fadeIn(1000);
		$.post("posts.php",{act:"signup",check:"user",username:$("#Username").val()},function(getResult){
			result = getResult;
		});
			// check for user
			if(result == "DONE"){
				$("#div_user").fadeTo(200,0.1,function(){
				  $(this).html('Username exists!').removeClass().addClass('done').fadeTo(900,1);});
			// user else	
			}else{
				$("#div_user").fadeTo(200,0.1,function(){
				  $(this).html('Username already taken!').removeClass().addClass('error').fadeTo(900,1);});
			return false;
			}
	}


What I want to do is if the username is valid or not taken yat then PHP page returns DONE value and I am using that value if it is done then add valid text in div if not then add invalid text in div and if it is invalid then the function should return false value and alert the user for changing the username, but function is not returning the value neither true not false.

Thanks :)

This post has been edited by noorahmad: 20 November 2009 - 09:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: ((Sloved)) function can't return any value

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: ((Sloved)) function can't return any value

Posted 18 November 2009 - 01:19 AM

The function you perform the AJAX call from will not be alive when the response returns from the server, that's why it is not working, make the callback-function do the stuff for you instead of trying to make the main-function return some value it will never receive. :)
Was This Post Helpful? 2
  • +
  • -

#3 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: ((Sloved)) function can't return any value

Posted 20 November 2009 - 10:06 PM

Thanks for you reply dear, I solved it with using $.ajax.
here is the code:
function user(){
		var Return = $.ajax({url:"posts.php?act=signup&check=user&username="+$("#Username").val(),async:false});
		if( Return.responseText == "DONE" ){
			return true;
		}else{
			return false;
		}		
	}


:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1