Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Return problem (with function)

    Hello

    I'm busy with a new mini-project but I'm new with Javascript and AJAX.
    I'm using a little bit of the Prototype Framework and the AJAX-object is a template from school.

    But I've got a some problems with my formvalidation script. I created for every check (like: DublicateName, DublicateEmail, ValidEmail, ...) a function. Now I want to make a function that checks all these earlier functions.

    File:
    http://pastebin.parentnode.org/32203

    Function example of the dublicate nickname
    Code:
    function CheckDublicateNickname() 
    {
    	var http = createREQ();
    	var divReturn = $('return');
    	http.onreadystatechange = function() {
    		if(http.readyState == 4) {
    			if (http.responseText == $('nickname2').value) {
    				divReturn.innerHTML = "<p class='reg_error'>Nickname bezet!</p>";
    				document.nicknameSrc.src = "img/ico.cross.png";
    				$('nickname2').focus();
    				return false;
    			} else if (http.responseText == "No Result") {
    				divReturn.innerHTML = "";
    				document.nicknameSrc.src = "img/ico.accept.png";
    				return true;
    			} else if ($('nickname2').value == '') {
    				divReturn.innerHTML = "<p class='reg_error'>Vul een nickname in!</p>";
    				document.nicknameSrc.src = "img/ico.cross.png";
    				$('nickname2').focus();
    				return false;
    			}
    		}
    	}
    	http.open("POST", "php/modules/CheckDublicateNickname.php");
    	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    	http.send("nickname=" + $('nickname2').value)
    }
    Overall function (this one need to check all the earlier functions)
    Code:
    function CheckRegistrationForm()
    {
    	var msg = "Volgende velden zijn niet of onjuist ingevuld: \n\n";
    	var error = false;
    	var nickname = $('nickname2').value;
    	var email = $('email').value;
    	var captcha = $('security_code').value;
    	
    	if (nickname == "") { 
    		msg += "- Nickname \n"; 
    		var error = true; 
    	} 
    	if (email == "") { 
    		msg += "- E-mailadres \n"; 
    		var error = true; 
    	}
    	if (captcha == "") {
    		msg +="- Controle \n";
    		var error = true;
    	}
    	if (error) { 
    		alert(msg); return false;
    	}
    	if (CheckDublicateNickname() == true) {
    		alert("test");
    	}
    }
    Now when CheckDublicateNickname () is true, I want to store some data to a MySQL database with AJAX (this is not a problem). The problem is that my function: CheckDublicateNickname () doesn't give a true of false. At every if and else function is a return with true of false...

    What is the solution?
    I hope you can help.

    Grtz,
    Moby

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Code:
    function CheckDublicateNickname() 
    {
    	var http = createREQ();
    	var divReturn = $('return');
    	http.onreadystatechange = function() {
    		if(http.readyState == 4) {
    			if (http.responseText == $('nickname2').value) {
    				divReturn.innerHTML = "<p class='reg_error'>Nickname bezet!</p>";
    				document.nicknameSrc.src = "img/ico.cross.png";
    				$('nickname2').focus();
    				return false;
    			} else if (http.responseText == "No Result") {
    				divReturn.innerHTML = "";
    				document.nicknameSrc.src = "img/ico.accept.png";
    				return true;
    			} else if ($('nickname2').value == '') {
    				divReturn.innerHTML = "<p class='reg_error'>Vul een nickname in!</p>";
    				document.nicknameSrc.src = "img/ico.cross.png";
    				$('nickname2').focus();
    				return false;
    			}
    		}
    	}
    	http.open("POST", "php/modules/CheckDublicateNickname.php");
    	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    	http.send("nickname=" + $('nickname2').value)
    	return SOME_BOOLEAN_VALUE;
    }
    Trinithis

  • #3
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When I add this:
    Code:
    function CheckDublicateNickname() 
    {
    	var http = createREQ();
    	var divReturn = $('return');
    	http.onreadystatechange = function() {
    		if(http.readyState == 4) {
    			if (http.responseText == $('nickname2').value) {
    				divReturn.innerHTML = "<p class='reg_error'>Nickname bezet!</p>";
    				document.nicknameSrc.src = "img/ico.cross.png";
    				$('nickname2').focus();
    				return false;
    			} else if (http.responseText == "No Result") {
    				divReturn.innerHTML = "";
    				document.nicknameSrc.src = "img/ico.accept.png";
    				return true;
    			} else if ($('nickname2').value == '') {
    				divReturn.innerHTML = "<p class='reg_error'>Vul een nickname in!</p>";
    				document.nicknameSrc.src = "img/ico.cross.png";
    				$('nickname2').focus();
    				return false;
    			}
    		}
    	}
    	http.open("POST", "php/modules/CheckDublicateNickname.php");
    	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    	http.send("nickname=" + $('nickname2').value)
    	return true;
    }
    The function gives me the alert... But I want to have the returns insde the if(http.readyState == 4) { because I need the reaction (http.responseText) from my PHP file to validate and not from the whole functions...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •