2016-07-28 157 views
0

我的問題是,我無法訪問我的主要 頁面中包含的php變量從ajax調用的php文件。HIGHCHARTS - jquery/Ajax訪問php變量從ajax調用的PHP文件

是否有訪問它的方式或者我應該在我的PHP文件中的PHP文件阿賈克斯

PHP叫:variables.php

<?php 
    $myServername = "local_host"; 
    $myUsername = "user"; 
    $myPassword = "password"; 
    $myDbname = "dbname"; 
?> 

PHP:connection.php *連接變量被定義variable.php包括在我的主網頁

<?php 
    $conn = mysqli_connect($myServername, $myUser, $myPassword, $myDatabase); 

    // verify connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 
?> 

PHP的開頭:liveserverdata.php

<?php 
    header('Content-type: application/json'); 

    //GET MYSQL DATA 

    // Create connection 
    include ("connection.php"); 

    $sqlGetHoraire = "SELECT * FROM mytable ORDER by id ASC"; 

    $result = $conn->query($sqlGetHoraire); 

    while($row = $result->fetch_assoc()) 
    { 
     //DO THINGS HERE 
    } 

    // CALCULATE VALUES 

    // The x value is the current JavaScript time, which is the Unix time  multiplied by 1000. 
    $x = time() * 1000; 
    // The y value is the quatity of paper lost for this work period 
    $y = rand(0, 100); 

    // Create a PHP array and echo it as JSON (Date,Value) 
    $ret = array($x, $y); 


    // Data return to ajax function 
    echo json_encode($ret); 
?> 

JAVASCRIPT:LiveData.js

$(function() { 
var chart; // global 

function requestData() { 
    $.ajax({ 
     type: "POST", 
     url: 'liveserverdata.php', 
     success: function(point) { 
      //Action with the data from the php 
      var series = chart.series[0]; 
      var Shift = series.data.length > 20; // shift if the series is longer than 20 

      // add the point 
      chart.series[0].addPoint(eval(point), true, Shift); 

      //Change Title 
      chart.setTitle({text: "Title " + point[1]});  

      // call it again after one second 
      setTimeout(requestData, 1000); 
     }, 
     cache: false 
    }); 
} 

$(document).ready(function() { 
    chart = new Highcharts.Chart({ 
     chart: { 
      renderTo: 'MyLiveData', 
      defaultSeriesType: 'spline', 
      events: { 
       load: requestData 
      } 
     }, 
     xAxis: { 
      type: 'datetime', 
      tickPixelInterval: 150, 
      maxZoom: 20 * 1000 
     }, 
     yAxis: { 
      minPadding: 0.2, 
      maxPadding: 0.2, 
      title: { 
       text: 'Pied', 
      } 
     }, 
     series: [{ 
      name: moment().format('DD MM YYYY'), 
      data: [] 
     }] 
    });  
}); 
}); 

HTML/PHP:的index.php *我的主網頁

<?php include ("variables.php"); ?> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="UTF-8"> 
     <script src = "LiveData.js"></script> 
    </head> 
    <body class="mybody"> 
     <div id="MyLiveData" class="section-chart"></div> 
    </body> 

THX很多

丹尼爾

解決方案:直接在我的php文件中調用ajax包含variable.php。

PHP:connection.php

include ("variables.php") 
<?php 
    $conn = mysqli_connect($myServername, $myUser, $myPassword, $myDatabase); 

    // verify connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 
?> 
+1

您沒有在任何地方定義的'mysqli_connect'中使用的變量。 – aynber

+0

@aynber已更新 –

+1

你檢查了你的錯誤日誌嗎?它只是沒有返回數據,還是返回某種錯誤? – aynber

回答

1

變量只能從他們所在的範圍訪問當你做一個AJAX調用另一個頁面,該頁面無法訪問比你送什麼它的任何其他。以及它自己收集的內容。它不是調用頁面範圍的一部分。你需要在你的connection.php中包含variables.php(所以當連接需要被創建時總是存在的),或者你的liveserverdata.php。

+0

Thx很多!現在完美工作:) –