1

How can I use some data as global? For example:

$.ajax({
      type: 'GET',
      url: "php/setting.php",
      data: { name: name, lastname: lastname, firstname: firstname
      }
      dataType: 'html',
      context: document.body,
      async: false,
      success: function(data) {
        //how to get this name,lastname, and firstname
      }
});

How can I get this name, lastname, firstname, and use it as a global variable like:

 if(name == 'archie') {
 }

thanks.

vvns
  • 3,548
  • 3
  • 41
  • 57
reyes
  • 67
  • 1
  • 2
  • 8
  • 3
    Do not perform synchronous ***Asynchronous Javascript And XML (AJAX)***, remove the async : false, and read -> http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – adeneo Sep 10 '13 at 09:36
  • Put the ajax call inside a function and declare those variables inside that function as well. That way they'll be accessible in the success callback. – Reinstate Monica Cellio Sep 10 '13 at 09:36
  • http://stackoverflow.com/questions/3352020/jquery-the-best-way-to-set-a-global-variable – Nibin Sep 10 '13 at 09:37
  • thank you sir for answering my question. got some ideas in those links. – reyes Sep 10 '13 at 09:45

2 Answers2

3
var name="the name";
var lastname="the lastname";
var firstname="the firstname";

$.ajax({
  type: 'GET',
  url: "php/setting.php",
  data: { name: name, lastname: lastname, firstname: firstname},
  dataType: 'html',
  context: document.body,
  async: false,
  success: function(data) {
    if(name == 'archie') {
         //you can use it
    }
  }
  });
suhailvs
  • 20,182
  • 14
  • 100
  • 98
  • thank you sir for answer my question, the thing is i want the name to be global, so i can call it any time. – reyes Sep 10 '13 at 09:46
  • @reyes Prefix the variable names with `window.`, and do some reading about variable scope in Javascript - http://stackoverflow.com/questions/500431/javascript-variable-scope – Reinstate Monica Cellio Sep 10 '13 at 09:47
  • @reyes if you define the variable at very beginning of script, then you can use it anywhere. for example if you define `var name;` just below `$(document).ready(function() {`, then you can use `name` anywhere inside the `document.ready` function – suhailvs Sep 10 '13 at 09:49
  • @suhail how can i get the name in the ajax and call it any time. is this possible success: function(data) {var name=data.name}? – reyes Sep 10 '13 at 09:53
  • @reyes `data` is the value return from `php/setting.php` script. i am not good at php. may be you may need to return array {'name':"sdffds"...} in `setting.php`. then you can use `data.name` – suhailvs Sep 10 '13 at 09:59
0

I don't know if I fully understand the question but I think what you want to do is to set up an empty variable in your namespace (or in the global namespace if that's how you're doing it) such as var name; and then when you perform your AJAX request you want to do something like

success: function(data) {
    name = data.name
  }
BenM
  • 4,218
  • 2
  • 31
  • 58