-4
    var randomColorFactor = function() {
        return Math.round(Math.random() * 255);
    };
    var randomColor = function(opacity) {
        return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
    };

    var config = '<div id='humidity'>'{
        type: 'line',
        data: {

            labels: <?php echo $json_array1; ?>,

            datasets: [{
                label: "",
                data: <?php echo $json_array; ?>,
            }]
        },

        options: {
            responsive: true,
            title:{
                display:true,
                text:'Chart'
            },
            tooltips: {
                mode: 'label',
                callbacks: {

                }
            },
            hover: {
                mode: 'dataset'
            },
            scales: {
                xAxes: [{
                    display: true,
                    scaleLabel: {
                        display: true,
                        labelString: 'Month'
                    }
                }],
                yAxes: [{
                    display: true,
                    scaleLabel: {
                        display: true,
                        labelString: 'Humidity'
                    },
                    ticks: {
                        suggestedMin: -10,
                        suggestedMax: 250,
                    }
                }]
            }
        }
    };

    $.each(config.data.datasets, function(i, dataset) {
        dataset.borderColor = randomColor(0.4);
        dataset.backgroundColor = randomColor(0.5);
        dataset.pointBorderColor = randomColor(0.7);
        dataset.pointBackgroundColor = randomColor(0.5);
        dataset.pointBorderWidth = 1;
    });

    window.onload = function() {
        var ctx = document.getElementById("canvas").getContext("2d");
        window.myLine = new Chart(ctx, config);
    };

In this code I want to change $json_array to some other array $json_array2 on mouse click. How can I achieve it? The $json_array contains the values fetched from the database.

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
Ayush Garg
  • 13
  • 2
  • 4

2 Answers2

1

PHP is server-side and jQuery (JS) is client-side language, you can't change PHP variable after it executes.

You can use AJAX, to load new data.

Martin Hučko
  • 791
  • 5
  • 16
  • 1
    This should be a comment , answer should be detailed and knowledgable, answer should be given , if you have an example for to show to the op about how he can achieve that. – Arsh Singh Jul 15 '16 at 08:11
  • 1
    I disagree. I think it's an answer to the question raised. Q: "how to change php variable on mouseclick?" A: "You can use AJAX to load new data". It may not be enough to solve the issue; if so the OP is free to select another answer. But it does answer the question, and gives enough info for an enterprising OP to find out how to do what's needed. – BeetleJuice Jul 15 '16 at 08:32
  • I agree with the points raised by BeetleJuice and find Arsh being harsh with those downvotes. – PockeTiger Jul 15 '16 at 08:47
  • @PockeTiger I see what you did there – BeetleJuice Jul 15 '16 at 09:16
1

You can't change a variable directly in PHP with a mouse click, PHP runs in your server, you only have access to PHP code until it runs in your server and returns the response (usually HTML).

If you want to send data to the back end though (maybe to store in the database), you can do that with Ajax.

Here are a couple of great tutorials: http://www.w3schools.com/jquery/jquery_ajax_intro.asp http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php

Rinon
  • 579
  • 5
  • 14
  • 1
    This should be a comment , answer should be detailed and knowledgable, answer should be given , if you have an example for to show to the op about how he can achieve that. – Arsh Singh Jul 15 '16 at 08:12
  • @ArshSingh Thanks, just added a couple of links. – Rinon Jul 15 '16 at 08:13