SirBdon
SirBdon

Reputation: 169

Use output value from php as variable in javascript?

I'm trying to take a value that a php page outputs and use it later as a variable in a calculation. Currently I am trying this:

var price = function() {
      $.get('gox.php')
  }


function toDol(elem) {
    var btcToDol = parseFloat(elem.value) * price || '';
    document.getElementById('dol').value = btcToDol.toFixed(2);
}

function toBtc(elem) {
    var dolToBtc = parseFloat(elem.value) / price || '';
    document.getElementById('btc').value = dolToBtc.toFixed(4);
}

The important part is I want the 'price' variable to equal the value gox.php outputs (e.g. 99.9999) so that I can use it later to do the math in functions 'toDol' and 'toBtc'.

Thank you for your help!

Upvotes: 1

Views: 68

Answers (2)

Renegade91127
Renegade91127

Reputation: 123

Try the following code

var price=$.ajax({
    type: 'GET',
    url: 'gox.php',
    global: false,
    async:false,
    success: function (data) {return data;}
}).responseText;

I always have issues with $.get and $.post

Upvotes: 0

elzaer
elzaer

Reputation: 729

var price = 0;
$.get('gox.php').done(function(data) {
  price = data;
});

Upvotes: 4

Related Questions