get variable from php file import it and use it in ajax

0

I have a php file with the following content

----filename.php---

<?php
$text = 'eos';
?>

and I have a js file called app.js with the following content

$(document).ready(function(){
    $.ajax({
        url: "http://127.0.0.1/swapngo.org/proyecta_json.php",
        method: "GET",
        success: function(data) {
            console.log(data);
            var tiempo = [];
            var coin = [];


            for(var i in data) {
                tiempo.push("Date " + data[i].tiempo);
                coin.push(data[i].VARIABLE-PHP);
            }

var chartdata = {
                labels: tiempo,
                datasets : [
                    {
                        label: 'Coin', 
                        backgroundColor: 'rgba(78, 153, 224)',
                        borderColor: 'rgba(200, 200, 200, 0.75)',
                        hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                        hoverBorderColor: 'rgba(200, 200, 200, 1)',
                        data: coin
                    }
                ]
            };

        var ctx = $("#mycanvas");

            var barGraph = new Chart(ctx, {
                type: 'line',
                data: chartdata
            });
        },
        error: function(data) {
            console.log(data);
        }
    });
});

What solution could I use to transport the value of the variable $ text in the php file and use it in my ajax code? I appreciate the help you can give me.

Thanks

I edit my question to show you in more detail what I'm trying to achieve

// peticion ajax 
var url = "otro.php";
$.ajax({
url: url,
type: "GET",
success: function (respuesta) { 
    console.log(respuesta);
}
});


$(document).ready(function(){
$.ajax({
    url: "http://127.0.0.1/swapngo.org/proyecta_json.php",
    method: "GET",
    success: function(data) {
        console.log(data);
        var tiempo = [];
        var coin = [];


        for(var i in data) {
            tiempo.push("Date " + data[i].tiempo);
            coin.push(data[i].console.log(respuesta));  <------
        }

var chartdata = {
            labels: tiempo,
            datasets : [
                {
                    label: 'Coin', 
                    backgroundColor: 'rgba(78, 153, 224)',
                    borderColor: 'rgba(200, 200, 200, 0.75)',
                    hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                    hoverBorderColor: 'rgba(200, 200, 200, 1)',
                    data: coin
                }
            ]
        };

    var ctx = $("#mycanvas");

        var barGraph = new Chart(ctx, {
            type: 'line',
            data: chartdata
        });
    },
    error: function(data) {
        console.log(data);
    }
});
});

I want to place the value I got from the AJAX query there to generate the Grafica. Thank you for your help.

    
asked by Rafuch0 02.08.2018 в 02:15
source

2 answers

5

To collect a PHP variable in an AJAX it is enough to do a simple 'echo' of this variable, here is an example of:  
Simple GET request:

// peticion ajax 
var url = "filename.php";
$.ajax({
    url: url,
    type: "GET",
    success: function (respuesta) { 
        console.log(respuesta);
    }
});

In filename.php :

<?php
    $texto = "Texto mandando desde PHP";
    echo $texto; 
?>

You can also send data in POST and depending on it send different answers:
Send parameters via POST:

// peticion ajax 
var url = "filename.php";
var enviarVariable = "dia";
$.ajax({
    url: url,
    data: { "parametro1":enviarVariable },
    type: "POST",
    success: function (respuesta) { 
        console.log(respuesta);
    }
});

In filename.php :

<?php
    $paramentro = $_POST["parametro1"];
    if($paramentro=="dia"){
        echo "Abierto";
    } else {
        echo "Cerrado";
    }
?>

And if you need:
Send an ARRAY in JSON format:
To be able to return an array in JSON format you have to convert it when sending it in filename.php ;

echo json_encode($array);

And prepare the AJAX to receive it, adding the parameter to the AJAX request:

dataType: "json"
  

You can only send in JSON format if it is to the same machine, AJAX requests that go outside the machine have cross-domain problem so they are restricted by default by the browser so that you need to use the JSONP

format
    
answered by 02.08.2018 в 08:49
0

What I did in the end was to separate the whole set of files the php that capture the data and eject it in json format run by cron and it shows separately the graph in the php that requests it using a if that condition the The user's entry separates the entire set of graphics depending on the option.

    
answered by 08.09.2018 в 04:17