...

View Full Version : Return problem (with function)



Moby
06-20-2008, 10:08 PM
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

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)

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

Trinithis
06-20-2008, 10:12 PM
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;
}

Moby
06-20-2008, 10:43 PM
When I add this:

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum