如何将javascript feed数据传输到php变量?

时间:2019-01-27 04:59:47

标签: javascript php

我需要从javascript feed数据中获取市场数据,并将其转换为php变量,以便以后存储在数据库中。

这是javascript数据供稿代码和php页面:

      `  <script type="text/javascript" src="https://files.coinmarketcap.com/static/widget/currency.js">
    </script>
    <div class="coinmarketcap-currency-widget" data-currencyid="1"          data-base="USDT" data-secondary="BTC" data-ticker="true" data-rank="true"     data-marketcap="true" data-volume="true" data-stats="USD" data-statsticker="true">


        if (window.XMLHttpRequest){
            xmlhttp=new XMLHttpRequest();
     }

     else{
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

    var PageToSendTo = "sendprice.php?";
     var MyVariable1 = "data-base";
     var MyVariable2 = "data-secondary";
     var MyVariable3 = "data-stats";
     var MyVariable4 = "data-base";

     var VariablePlaceholder = "btcprice";
      var UrlToSend = sendprice.php? + data-base + btcprice;

      xmlhttp.open("GET", UrlToSend, false);
     xmlhttp.send();



   <?php

 $btcprice = $_GET['btcprice'];
  ?>
     <!DOCTYPE HTML>
   <html>
   <head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="Portal Associates" />

       <title>Untitled 3</title>
      </head>

     <body>
      <?php
       echo $btcprice;
       ?>

       </body>
       </html>

`

我希望能够捕获实时数据并将其存储在db中以生成图表。我不是JavaScript程序员。请赐教。

预先感谢, 巴图

2 个答案:

答案 0 :(得分:0)

您将需要使用AJAX与php通信。请看以下链接以开始使用:https://www.w3schools.com/php/php_ajax_php.asp

答案 1 :(得分:0)

尝试一下:

JavaScript:

var MyVariable1 = "data-base";
var MyVariable2 = "data-secondary";
var MyVariable3 = "data-stats";
var MyVariable4 = "data-base";
var VariablePlaceholder = "btcprice";
var UrlToSend = "sendprice.php";

var params = VariablePlaceholder + '=' + MyVariable1 + '&MyVariable2=' + MyVariable2 + '&MyVariable3=' + MyVariable3 + '&MyVariable4=' + MyVariable4;

xmlhttp.open( "GET", UrlToSend + '?' + params, false );
xmlhttp.send();

PHP(在sendprice.php中):

$btcprice = $_GET['btcprice'];
$MyVariable2 = $_GET['MyVariable2'];
$MyVariable3 = $_GET['MyVariable3'];
$MyVariable4 = $_GET['MyVariable4'];