Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to put a PHP math result into JavaScript chart

I’ve been trying to figure this out for days. I hope I can explain this clear. I am trying to call up the results of this simple math equation in JavaScript code below:

             <?php  
             $x=37;  
             $y=15;  
             $z=$x-$y;  
             echo " ",$z;  
             ?> 

part of the script code

        { y: $z, label: "slice" },
        { y: 2, label: "pepper" },
        { y: 15, label: "sprite" },
        { y: 23, label: "coke" }

I now trying to put the $z in the script code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i tried using JavaScript and call tags, but that just messes up the JavaScript code. It only works if the number is posted only. Let know if what I did is possbile

>Solution :

To pass the value of $z from PHP to JavaScript, you can include the value of $z in a JavaScript block in your PHP code:

<?php  
$x = 37;  
$y = 15;  
$z = $x - $y;  
echo '<script>var z = ' . $z . ';</script>';  
?> 

<script>
// use the variable z in your JavaScript code
var dataPoints = [
    { y: z, label: "slice" },
    { y: 2, label: "pepper" },
    { y: 15, label: "sprite" },
    { y: 23, label: "coke" }
];
</script>

In this example, the PHP code outputs a JavaScript block that sets the value of a variable called "z" to the value of $z. The JavaScript code then uses the "z" variable in the dataPoints array.

This approach should allow you to use the value of $z in your JavaScript code.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading