This is the code and this is a variable price value ( parseFloat(preco).toFixed(4); ) thats refresh every 2 seconds i need to pass this to a php variable, how can I do it? I have tried various ways but it does not work for me. It must be something so simple and I'm overlooking it.
var preco;
var valor;
var HttpClient = function() {
this.get = function(aUrl, aCallback) {
var anHttpRequest = new XMLHttpRequest();
anHttpRequest.onreadystatechange = function() {
if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
aCallback(anHttpRequest.responseText);
}
anHttpRequest.open( "POST", aUrl, true );
anHttpRequest.send( null );
}
}
var HttpClient2 = function() {
this.get = function(aUrl, aCallback) {
var anHttpRequest = new XMLHttpRequest();
anHttpRequest.onreadystatechange = function() {
if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
aCallback(anHttpRequest.responseText);
}
anHttpRequest.open( "GET", aUrl, true );
anHttpRequest.send( null );
}
}
var HttpClient3 = function() {
this.get = function(aUrl, aCallback) {
var anHttpRequest = new XMLHttpRequest();
anHttpRequest.onreadystatechange = function() {
if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
aCallback(anHttpRequest.responseText);
}
anHttpRequest.open( "GET", aUrl, true );
anHttpRequest.send( null );
}
}
var client = new HttpClient();
var client2 = new HttpClient2();
var client3 = new HttpClient3();
let paragrafo = document.createElement("p");
paragrafo.setAttribute("id", 'dracoVlr');
let total = document.createElement("p");
total.setAttribute("id", 'dracodolares');
let totalr = document.createElement("p");
totalr.setAttribute("id", 'dracoreais');
let totale = document.createElement("p");
totale.setAttribute("id", 'dracoeuro');
let inputvlr = document.createElement("input");
inputvlr.setAttribute("id", 'dracoInput');
inputvlr.setAttribute("type", "number")
inputvlr.setAttribute("placeholder", "Cantidad de DRACOS")
inputvlr.setAttribute("value", localStorage.getItem('dracoVlr1'))
let texto = document.createTextNode("Precio DRACO" ' - ' parseFloat(preco).toFixed(4));
paragrafo.appendChild(texto);
let body = document.body;
body.appendChild(paragrafo);
body.appendChild(inputvlr);
body.appendChild(total);
body.appendChild(totalr);
body.appendChild(totale);
function display() {
client.get('https://api.mir4global.com/wallet/prices/draco/daily', function(response) {
var retorno = JSON.parse(response);
preco = retorno.Data[retorno.Data.length - 1].USDDracoRate
var input = document.querySelector('input');
valor = input.value;
localStorage.setItem('dracoVlr1', input.value);
document.title = 'Draco' ' - $' parseFloat(preco).toFixed(4);
document.getElementById('dracoVlr').innerHTML = "Preço do Draco" ' - $' parseFloat(preco).toFixed(4);
});
client2.get('https://api.binance.com/api/v3/ticker/price?symbol=BUSDBRL', function(response) {
var binance = JSON.parse(response);
console.log(binance.price)
document.getElementById('dracodolares').innerHTML = 'Total em Dólares: $' (valor * preco).toFixed(4)
document.getElementById('dracoreais').innerHTML = 'Total em Reais: R$' ((valor * preco)*binance.price).toFixed(4)
});
client3.get('https://api.binance.com/api/v3/ticker/price?symbol=EURBUSD', function(response) {
var binance3 = JSON.parse(response);
console.log(binance3.price)
document.getElementById('dracodolares').innerHTML = 'Total em Dólares: $' (valor * preco).toFixed(4)
document.getElementById('dracoeuro').innerHTML = 'Total en Euros: EUR' ((valor * preco)*binance3.price).toFixed(4)
});
}
var results= parseFloat(preco).toFixed(4);
<?php $results = "<script>document.write(results)</script>"?>
const createClock = setInterval(display, 2000);
I would be very grateful if you could help me please.
CodePudding user response:
This is a simple example of how can pass Javascript variables to PHP you can understand how to pass variables to PHP. According to your script let texto = document.createTextNode("Precio DRACO" ' - ' parseFloat(preco).toFixed(4));
this is the Javascript variable and as example i pass this to like <?php echo "<script>window.alert(texto)</script>"; ?>
CodePudding user response:
Ajax example for it in your PHP file request $result = $_POST['variable'];
let texto = document.createTextNode("Precio DRACO" ' - ' parseFloat(preco).toFixed(4));
$.ajax({
type: 'POST',
url: 'yourphpfile.php',
data: {
'variable': texto
},
});
$result = $_POST['variable'];