var num = 1; $.post("mailsender.php", data, function(data){ num++; }) 

As a result, nothing happens, it does not increase. The question is: how is it called and how to avoid it, if there is an opportunity to post links about it to read. I want to make a captcha on ajax, but due to the fact that nothing can be transferred from $ .post, it is impossible to verify the validity of data entry from a captcha. Thank!

  • How do you check if it works or not? num ++; alert (num); So also does not work? - Sh4dow
  • Well, I’m an alert and I’m checking it, but the increase doesn’t come from $ .post I tried it this way: var num = 0; function numeric () {num ++; } $ .post ("mailsender.php", data, function (data) {numeric ();}) So, too, why it does not work. Maybe I make a mistake somewhere? - anton
  • In question, such things are added. This is not an answer , ok? Essentially check that the callback is generally executed (data is not empty, the address is valid). alert there add. - Sh4dow

3 answers 3

So, this .... $ .post is asynchronous :) How can you know when it will work? )))) It increases the variable, but not the fact that this happens BEFORE your check :)

  • Well, yes it is logical ... damn, what then to come up with: / - anton
  • And why do you need to know the value of num? Describe the task in more detail - I will tell you how best to do it :) - Stanislav Komar

I think that here you will find everything that interests you by $ .post

    I offer an option:

    pass num in the request to the server and from there through the data to the function. Next, n ++, then write the value to some DOM element as the value attribute and read it from outside.