gpt4 book ai didi

javascript - HIGHCHARTS - Jquery/Ajax 从 ajax 调用的 php 文件访问 php 变量

转载 作者:行者123 更新时间:2023-12-03 06:26:16 24 4
gpt4 key购买 nike

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

有没有办法访问它,或者我应该将 php 文件包含在我的 ajax 调用的 php 文件中

PHP:变量.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>

非常感谢

丹尼尔

解决方案:将variable.php直接包含在我的ajax调用的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);
}
?>

最佳答案

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

关于javascript - HIGHCHARTS - Jquery/Ajax 从 ajax 调用的 php 文件访问 php 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38640256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com