Jquery.get () - problem using data as global variables

Well, I read a lot on the Web, still haven't found a solution for my problem: I need to check if the contents of the file are "nok" or empty ("") using the jquery get method. One of the things I tried (doesn't work of course, but clearly shows my idea):

$(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 to really store a string from data into a global variable (or other suitable structure) so that plain nok text can be used in if statements.

+3
source share
2 answers

Use window.variablenameor in jQuery style $.variablenameto assign and access global variables.

eg.

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

        hasError = ($.captchacheck == "nok");
+4

ready, . var . :

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

Source: https://habr.com/ru/post/1721942/


All Articles