'price' value won't be set until the callback completes, so try this: unction calc_matrix(x, y) { loc = "index.php?module=product";
var price = 0; $.post(loc, { "request":"xmlhttp", "action":"calc_pricematrix", "x":x, "y":y }, function(data) { price = Math.round(parseFloat(data)*100)/100; alert(price); }); On May 13, 1:02 am, V <vincenti...@gmail.com> wrote: > I can't get global variables work, maybe somebody knows the answer > because this should be very simple. > > The following does not work, the price is not available at the end of > the function in the alert. > > function calc_matrix(x, y) > { > loc = "index.php?module=product"; > > var price = 0; > $.post(loc, > { > "request":"xmlhttp", > "action":"calc_pricematrix", > "x":x, > "y":y > }, > function(data) > { > price = Math.round(parseFloat(data)*100)/100; > > }); > alert(price); > > }