【问题标题】:HIGHCHARTS - Jquery/Ajax access php variables from the php file called by ajaxHIGHCHARTS - Jquery/Ajax 从 ajax 调用的 php 文件中访问 php 变量
【发布时间】:2016-07-28 15:08:44
【问题描述】:

我的问题是我无法访问包含在我的 main 中的 php 变量 ajax调用的php文件中的页面。

有没有办法访问它,或者我应该在我的 ajax 调用的 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>

非常感谢

丹尼尔

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

PHP : 连接.php

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

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

【问题讨论】:

  • 您没有在任何地方定义mysqli_connect 中使用的变量。
  • 你检查过你的错误日志吗?它只是不返回数据,还是返回某种错误?
  • 非常感谢 :) 已解决 - 问题是我不能直接使用其他 php 文件中的 php 变量!我稍后会发回我的解决方案
  • @aynber 非常感谢我发现了我真正的问题。我已经更新了我的问题你知道怎么做吗?

标签: javascript php jquery ajax highcharts


【解决方案1】:

变量只能在它们所在的范围内访问。当您对另一个页面进行 ajax 调用时,该页面无法访问除了您发送的内容以及它自己收集的内容之外的任何内容。它不是调用页面范围的一部分。您需要将 variables.php 包含在您的 connection.php 中(因此在需要建立连接时它始终存在),或者包含在您的 liveserverdata.php 中。

【讨论】:

  • 非常感谢!现在完美工作:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多