你能拥有多个ajax吗?

时间:2018-03-04 19:54:53

标签: javascript php html ajax highcharts

我正在使用ajax从慢速数据库中提取数据,这需要2.5秒。它成功更新了网页标签,但是当我尝试将其传递给Highcharts规格图时,毫不奇怪它失败了。我想如果我可以更新全局然后将它传递给它可以工作的图表,它没有。我试图延迟图表直到ajax完成,这失败了。 我认为问题是我似乎无法更新全局。 我想发布一些代码,但上次我试图将HTML放在这个网站上它出错了。

你的Simon M。

 // JavaScript Document
 // simple ajax to get data
    $(function () 
                {
                $.ajax({
                            url: 'extractLatest.php',   // call php file

                    success: function(datafromphp){ // when it is sucessful
        $( "#latest_mb" ).html(                 // replace html tag <div id="latest_mb"> contents 
                         datafromphp                                    // with whats returned from php
                            );


                                    var position    = "#gauge-chart-a"; // html container name
    // == start of chart == //
        $(function() {
          $(position).highcharts({
              chart: {
                type: 'gauge',
              },
              title: {
               text: 'Last Reading: '+ datafromphp +'mB at '+ msToTime(latest_UTC)
              },

                  pane: {
                startAngle: -150,
                endAngle: 150,
              },
              // the value axis
              yAxis: { // Month
                        // find scale max and min to nearest 10
                min: ((month_lo_mb/10)-0.5).toFixed(0)*10,
                max: ((month_hi_mb/10)+0.5).toFixed(0)*10,
                    title:{text:'mB'}, 

            plotBands: [

                    { // Month
        from: month_lo_mb,
          to: month_hi_mb,
        color: '#9DB6F9'}, // Blue

                    { // Week (Sun thro Sat)
        from: wk_lo_mb,
          to: wk_hi_mb,
        color: '#55BF3B'}, // green

                    { // Day
        from: day_lo_mb,
          to: day_hi_mb,
        color: '#FFFF63'}, // yellow

                    { // Day Avg
                // display day average line in red
        from: day_av_mb-0.125,
          to: day_av_mb+0.125,
        color: '#ff0000'} // red

                ]},
              series: [{ data: [datafromphp] }] // current
            }); // end of chart
        });
    }});
  });

这是我最接近所需的

<?php
//------------------------------------------------------------------------- 
// Latest
 $latest_mb = mysqli_fetch_array(mysqli_query($dbconnect, "\n"
  ." SELECT UTC,mB \n"
  ." FROM thundersense  \n"
  ." ORDER BY utc DESC LIMIT 1"));

echo $latest_mb[1];
echo sprintf("<h1>Last reading: %1\$.3fmB<br></h1>",$latest_mb[1]);
echo "<h2><i> taken at " . date("H:i:s",$latest_mb[0]) . "</i></h2>";
?>

返回:

  < !--
  Start of data extraction
  -- >


982.976

1 个答案:

答案 0 :(得分:0)

正如我所想,我缺乏经验,Ajax片段

    series: [{ data: [latest_array[1]] }]

并添加到highcharts ......

echo json_encode($return,JSON_NUMERIC_CHECK);

PHP返回

<%@ Language="VBscript" %>

<%
'declare the variables that will receive the values 
 Dim name 
 Dim idnum 
 Dim product
 Dim entrydate
 Dim area
 Dim qunaity

 'receive the values sent from the form and assign them to variables
  quantity=Request.Form("quantity")
  area=Request.Form("area")
  entrydate=Request.Form("date")

  stack over
 'let's now print out the received values in the browser
  Response.Write("Name: " & name & "<br>")
  Response.Write("Quantity: " & quantity & "<br>")
  Response.Write("Area: " & area & "<br>")
  Response.Write("Date: " & entrydate & "<br>")  
  %>

现在可以了。只需要破解定期更新...... 你的Simon M。