0

AJAX/JS noob here. I have the following AJAX code that returns some data from a php service. I am trying to find out how to assign the data that returns from the php service to some JS variable which I later want to display on the UI.

Here is my AJAX code:

<script type="text/javascript">
var data1;  
   $(function(){
        setInterval(function() {
            $.get("get_location.php", function(data) {
            console.log(data);
            $('#data').html(data);
            data1 = data; 
            });
        }, 500); 
   });
alert(data1);
</script>

And I have a variable in another script instance:

<script>
    var some_var1;
    var some_var2;
</script>

I have two questions that I have been stuck on for a while:

  1. The way my PHP returns the data is though echo, meaning I do this in PHP:

    echo $var1; echo $var2;

Is this the right way to pass the data to AJAX given that I need it more structured?

  1. Assuming I did #1 correctly, how do I assign "data" from AJAX to some_var1/some_var2 in JS?
moshikafya
  • 3,190
  • 5
  • 22
  • 27
  • 1
    1. You probably want to look into [JSON](https://en.wikipedia.org/wiki/JSON). 2. There is only one way to assign a value to variable: `variable = value;`. The more challenging part with async code is when it's safe to *read* the variable. – Felix Kling Jun 20 '15 at 20:17
  • What is the 'value' here? 'data'? And where do I place this code? I have two separate 'script'. – moshikafya Jun 20 '15 at 20:21
  • 1
    `value` is whatever you want to assign. In your case it would be `data`. All scripts are executed in the same global scope. But again, setting variables inside an Ajax callback is not that useful since you won't know *when* the variables will have the new value. Have a look at http://stackoverflow.com/q/14220321/218196 instead. – Felix Kling Jun 20 '15 at 20:23
  • I am trying to test reading 'data' and it's not working. For example alert(data); does not do anything... – moshikafya Jun 20 '15 at 20:28
  • 1
    You might not have placed `alert(data)` correctly in your code. Can't help you without a *complete* example. – Felix Kling Jun 20 '15 at 20:29
  • Edited the code above. thanks! – moshikafya Jun 20 '15 at 20:30
  • 1
    Well, `data` is not defined where you are trying to access it. Here is a simplified example: `function foo(bar) { }; alert(bar);` Would you expect that to work? Did you have a look at the linked question at all? This might help you more: http://stackoverflow.com/q/23667086/218196 – Felix Kling Jun 20 '15 at 20:31
  • How about this? Still does not work...:/ – moshikafya Jun 20 '15 at 20:33
  • 1
    Again: Read the linked questions. There are all about that exact issue. – Felix Kling Jun 20 '15 at 20:33
  • Looking into these links now...thanks! – moshikafya Jun 20 '15 at 20:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81091/discussion-between-moshikafya-and-felix-kling). – moshikafya Jun 20 '15 at 21:09

1 Answers1

0

If I understand your question correctly, and you have already included JQuery in your html, you need to use javascript variables.

if your console.log(data) is actually logging something to the console, put var ajaxData;, or something before your JQuery function, and replace your console.log(data) it with ajaxData = data;.