OK, so the real question here is a bit different from the question I've been
trying to answer. :-)

In general JavaScript coding, what I've been saying is true: If you want to
write code that uses data downloaded by a $.ajax call, you need to put that
code in a function called from the $.ajax success callback - or in the
callback itself.

However, what you're doing is using the validation plugin to validate a
form. So the question in this specific case is "how do I get the validator
plugin to validate based on data downloaded via an ajax call", right?

The validator plugin has a specific interface built in for this. Instead of
calling $.ajax yourself and trying to somehow get the validation plugin to
use that data, you use the validation plugin's own 'remote' property, which
will make the $.ajax call for you.

Documentation and examples:

http://docs.jquery.com/Plugins/Validation/Methods/remote#options

Let me know if that helps!

-Mike

On Mon, Dec 14, 2009 at 9:33 AM, Jojje <jojjsus_chr...@hotmail.com> wrote:

> Ok, yes you can check it out at
> http://www.sonicconnection.se/se/index.php?site=_misc/register
>
> It´s in swedish but its the first field that uses the code to check if
> the username already exists, it makes a call to a php script and the
> response is equal to the username if it exists in the database, and if
> it does'nt exist it returns nothing.
>
> I put the parts of the script thats for the registration at the top
> and commented it. the script file is sc_functions.js
>
> Its a lot of repetetive code int there that i havent made into
> fuctions yet so you know haha :)
>
> Thanks again :)
>
> George
>
> On 14 Dec, 18:15, Michael Geary <m...@mg.to> wrote:
> > Looks like we have two threads going on the same topic. :-)
> >
> > To give you a real code example, I'd need to see the code that makes use
> of
> > that 'result' variable you're setting. Where is that code and what does
> it
> > look like?
> >
> > The bottom line is simple: whatever the code is that uses the 'result'
> > variable, that code needs to be made into a function, and you need to
> call
> > that function in the ajax success callback. Or, that function can *be*
> the
> > success callback.
> >
> > You can't just set a variable in an asynchronous callback and expect
> other
> > code somewhere else on the page to somehow "know" when your variable is
> > ready for use.
> >
> > It's not a question of "how far deep in nested functions can you return a
> > value". Any nested function can return a value; it doesn't matter how far
> > deeply your functions are nested. But that return value goes only to
> > *whoever called that function*. In the case of a $.ajax callback, the
> return
> > value is being passed back into the $.ajax code, which probably ignores
> the
> > value completely.
> >
> > The real issue is *when* the code is run.
> >
> > Can you post a URL to a complete test page? That would make it a lot
> easier
> > to suggest the right way to write your code.
> >
> > -Mike
> >
> > On Mon, Dec 14, 2009 at 5:48 AM, Jojje <jojjsus_chr...@hotmail.com>
> wrote:
> > > Hi! Yeah i got that tip before but i'm not sure i understand this.
> > > Could you perhaps give me an example with the code below :)
> >
> > > regards
> >
> > > George
> >
> > > On 14 Dec, 14:27, Rick van Hoeij <rickvho...@gmail.com> wrote:
> > > > because of the async of javascript I normally use callback functions
> > > > to do the value passing. So instead of return true or return false in
> > > > a ajax function, it's better to call a function with the true or
> false
> > > > as a parameter.
> >
> > > > Just my two cents ;)
> >
> > > > On 12 dec, 20:24, Jojje <jojjsus_chr...@hotmail.com> wrote:
> >
> > > > > How deep in the scope can you go in nested functions and still
> return
> > > > > a value? For example:
> >
> > > > > $.validator.addMethod('userCheck', function (value) {
> > > > >     $.ajax({
> > > > >         type: "POST",
> > > > >         url: "_scripts/send_message.php",
> > > > >         data: "action=checkuser& username=" + value,
> > > > >         success: function(msg) {
> > > > >             if (msg) {
> > > > >                 return false;
> > > > >             }
> > > > >             else {
> > > > >                 return true;
> > > > >             }
> > > > >         }
> > > > >     });
> >
> > > > > },"");
> >
> > > > > Why is´nt this working?
> >
> > > > > If i do this:
> >
> > > > > //Global
> > > > > var result;
> >
> > > > > $.validator.addMethod('userCheck', function (value) {
> > > > >     $.ajax({
> > > > >         type: "POST",
> > > > >         url: "_scripts/send_message.php",
> > > > >         data: "action=checkuser& username=" + value,
> > > > >         success: function(msg) {
> > > > >             if (msg) {
> > > > >                 result = false;
> > > > >             }
> > > > >             else {
> > > > >                 result = true;
> > > > >             }
> > > > >         }
> > > > >     });
> > > > >     return result;
> >
> > > > > },"");
> >
> > > > > i get the value back but it does not work properly, It gets the
> value
> > > > > from the textfield but on keyup it's always one letter short... so
> i
> > > > > have to type one more letter to get the right value. I´m stuck on
> this
> > > > > so any help would be greatly appreciated! :)
> >
> > > > > Thanks in advance
> >
> > > > > George
>

Reply via email to