3

Well, I've read a lot in the Net, bust still haven't found a solution to my problem: I need to check if the content of a file is "nok" or empty ("") using the jquery method get. One of the things I tried (didn't work of course, but shows clearly my idea) is:

$(document).ready(function(){
$("#submit").click(function(){
...
var captchacheck="";
$.get("/form.php", function(data){captchacheck=data;}));
if(captchacheck == "nok") hasError=true;
...

So please tell me how really to store the string from data into a global variable (or another suitable structure) so that the plain text nok could be used in if-clauses.

2 Answers 2

4

Use window.variablename or in jQuery style $.variablename to assign and access global variables.

E.g.

$(document).ready(function() {
    $.captchacheck = "";
    $("#submit").click(function() {
        $.get("/form.php", function(data) {
           $.captchacheck = data;
        });

        hasError = ($.captchacheck == "nok");
Sign up to request clarification or add additional context in comments.

1 Comment

This works perfectly! Thanks a lot to both of you, guys! You helped me very much!
0

If you declare the variable outside of the ready function, it will be a global. Assigning it a value without using var will affect the global variable. This should work:

var captchacheck=false,hasError=false;
$(document).ready(function(){
  $.get("/form.php",{},function(data){captchacheck=data;});
  });
//later...
if(captchacheck == "nok"){ hasError=true; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.