Forum Moderators: open

Message Too Old, No Replies

an interesting quandry - will it blend?

returning AJAX values for a boolean operation

         

httpwebwitch

6:58 pm on Aug 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



needing help.
I have two validating functions:

isnameunique()
isdomainunique()

each of them does a little trip back to the server to check if the value aready exists in a database. The PHP script they call returns a very tiny snippet of JSON indicating "true" or "false". These functions are great in isolation, because right now each has an onSuccess() event with some happy alerting inside.

here is the isnameunique() function:
I'm using mootools for my AJAX because i'm lazy and happy being so


function isnameunique(){
var myXHR = new XHR({method:'get',onSuccess:function(ret){
var json = eval("("+ret+")");
if (json.success == 'false'){
alert("that name is not unique. pick another one.");
}else{
// happiness. do nothing.
}
}})
myXHR.send('http://www.example.com/checkusername.php','name='+...);
}

isdomainunique() is pretty much the same.

this is OK, but now I want to validate against both conditions. To do that, I want to wrap both of these in a boolean condition, so my impressive procedure only executes when both validated:

if (isnameunique() && isdomainunique()){
// do something impressive
}

I only want to do that impressive thing if both the name and domain are unique. sounds simple, right?

Why is this challenging?
because the validating functions are not returning a boolean value! They have their own internal onSuccess functions.

First I tried forcing my onSuccess function to return true/false. However it seems to drift out of scope from the calling function (isnameunique()) and I haven't figured out how to achieve

(isnameunique() && isdomainunique())

here's my modified attempt:


function isnameunique(){
var myXHR = new XHR({method:'get',onSuccess:function(ret){
var json = eval("("+ret+")");
if (json.success == 'false'){
alert("that name is not unique. pick another one.");
[b]return false;[/b]
}else{
[b]return true;[/b]
// happiness. do nothing.
}
}})
myXHR.send('http://www.example.com/checkusername.php','name='+...);
}

That doesn't do it. the onSuccess function may return a bool, but isnameunique() doesn't. the onSuccess() function does not communicate back to the function that triggered it.

Granted I've only spent 2 hours on this but I'm already pleading for some expert guidance here... anyone?

httpwebwitch

7:26 pm on Aug 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



this is a synchroniticy problem.
after drawing it out as a flowchart, I've realized I need a "trigger" and a "listener". I can't really get a single function to gather two values returned via AJAX before proceeding.

here's something I figure might work:


f1(){
obj = new Object;
ajax1();
ajax2();
listen();
}

ajax1(){
// do request with onSuccess="ajax1_success"
}
ajax1_success(result){
obj.ajax1 = result;
}

ajax2(){
// do request with onSuccess="ajax2_success"
}
ajax2_success(result){
obj.ajax2 = result
}

function listen(){
// listen to object
if (obj.ajax1 && obj.ajax2){
// do something amazing!
}else{
setInterval(listen(),x); // recurse and wait
}
}

what do you think? will it blend?