You are not logged in.

#1 2007-08-19 16:43:52

sven
Member
Registered: 2005-02-01
Posts: 311

SOLVED - Problem with Ajax - Prototype, Scriptaculous and validation

I've been trying to learn to make a nice Javascript form validation with the help of Protoype, Scriptaculous and the validation addon (http://www.tetlaw.id.au/view/javascript … validation). All the standard checks went fine, but then I tried to implement a more advanced thing - to use Ajax to check if the login already exists.

Here's the validation part:

<script type="text/javascript">
  var valid = new Validation('signup', {immediate: true, onFormValidate: formCallback});
  Validation.addAllThese([
              ['validate-login-uniqueness', 'Login is already in use.', function(v) {
                return is_unique_login('index.php?{$gets}',$('signup_login').value);
              }]                        
   ]);
</script>

signup_login is the id of the login input field. $gets has all the GET-parameters so practically calls the same page on which the form exists. The response value is the "select count()" result, 0 or 1 from the database.

and then the Ajax function

function is_unique_login(url,login){
  var pars = "ajax=1&is_unique_login="+login;
  var value;
  new Ajax.Request(
    url,
    {
      method:'post',
            parameters: pars, 
      onComplete: function(resp){
        value = resp.responseText == 0 ? true : false;
      }
    }
  );
  alert(value);
  return value;
}

The problem is that value-attribute never gets true or false value at the onComplete part.

Last edited by sven (2007-08-20 19:40:00)

Offline

#2 2007-08-20 19:39:16

sven
Member
Registered: 2005-02-01
Posts: 311

Re: SOLVED - Problem with Ajax - Prototype, Scriptaculous and validation

I solved it! asynchronous:false was the thing I needed to add to options. This way the function waits for the request to complete and then assigns the value to the variable. I spotted it thanks to some tests I did with alert(). I saw that when executing alert took some extra time, the result was right.

Offline

Board footer

Powered by FluxBB