0

My problem is that i am not able to access php variables included in my main page from the php file called by ajax.

Is there a way to access it or i should include the php file in my php file called by ajax

PHP : variables.php

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

PHP : connection.php * the connection variables are define in variable.php included at the beginning of my main page

<?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 * my main page

<?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 a lot

Daniel

SOLUTION : include variable.php directly in my php file called by ajax.

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);
    } 
?>
4
  • 1
    You don't have the variables used in mysqli_connect defined anywhere. Commented Jul 28, 2016 at 15:16
  • 1
    Have you checked your error logs? Is it just not returning data, or is it returning an error of some sort? Commented Jul 28, 2016 at 15:37
  • Thanks a lot :) Solved - the problem was that i can't use php variables from other php files directly ! i will post back my solution later Commented Jul 28, 2016 at 16:03
  • @aynber Thx a lot I have find my real problem. i have update my question do you know how to do it ? Commented Jul 28, 2016 at 18:15

1 Answer 1

1

Variables are only accessible from the scope they're in. When you make an ajax call to another page, that page cannot access anything other than what you send it, and what it gathers on its own. It is not part of the calling page's scope. You need to include the variables.php either in your connection.php (so it's always there when the connection needs to be made), or in your liveserverdata.php.

Sign up to request clarification or add additional context in comments.

1 Comment

Thx a lot ! Work perfectly now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.