BaBna is correct, the alert is firing before the response has come
back, try adding the alert inside the callback. however, i would not
recommend removing the "var" keyword. as it is now, you have:
<script type="text/javascript">
var tb1=[];
$(document).ready(function() {
// code here
});
</script>
because "tb1" is being defined outside of any other functions, it is
already globally scoped. removing the "var" keyword isn't going to
change the scope (as its already where it should be if you need global
access). if you ever need to force a variable to be globally scoped,
NEVER omit the "var" keyword, as it can lead to some very difficult-to-
spot bugs. anytime you need to expose a variable globally you should
simply attach it to the window object (which is where the global scope
lives when Javascript runs in the browser).
DON'T DO THIS!!! :
<script type="text/javascript">
myGlobalVar = "foo"; // "var" keyword omitted, leads to dirty and
hard-to-follow code
</script>
INSTEAD, ATTACH AS PROPERTY OF WINDOW :
<script type="text/javascript">
window.myGlobalVar = "foo"; // does the same thing as the example
above, but makes it easy for you and others to see that the variable
is globally scoped.
</script>
On Jul 9, 12:41 pm, BaBna <[email protected]> wrote:
> As it seems you want your tb1 global, remove the "var" for declaring
> it.
> Also, your alert(tb1) is likely to be executed before tb1 is defined.
> You need to put your alert in the callback function if you want to be
> sure it works.
>
> On Jul 9, 2:47 pm, Vaidotas Zemlys <[email protected]> wrote:
>
> > Hi,
>
> > When trying out jquery, I ran into seemingly very simple problem. I
> > did a search on internet and did not get any satisfactory result. I am
> > suspecting that the mistake is very simple, so anyone more experienced
> > can help me.
>
> > What I am trying to do is load data from server, then present it in
> > the table, and plot it later. Since I am intending to use flot plugin,
> > and it accepts arrays as an argument, I thought that I load the file,
> > save it to some array, and then plot it when time comes (when user
> > presses something, etc.) So my javascript code goes like this:
>
> > <script type="text/javascript" src="js/jquery-1.3.2.js"></
> > script>
> > <script type="text/
> > javascript">
> > var tb1=[];
> > $(document).ready(function() {
> > $.post("data/echocsv.php", function(text) {
> > tb1=text.split("\n");
> > })
> > alert(tb1);
> > });
> > </script>
>
> > echocsv.php is very simple, it just grabs csv file (text file) and
> > returns it:
> > <?
> > $ff=file_get_contents("data.csv");
> > echo $ff;
> > ?>
>
> > Yet when I run this script, nothing happens. If I move the alert into
> > callback function, then everything is alright. I understand that it is
> > possible that ajax query is not performed before the alert. But I did
> > not find out how to know when the query is succesfull. From examples
> > in the web I saw, that .ajax call is more frequently used, maybe I
> > should use it also?
>
> > Thanks for any help,
>
> > Vaidotas