Echo in a JS script
Solved
CorentinRoche
Posted messages
287
Status
Membre
-
CorentinRoche Posted messages 287 Status Membre -
CorentinRoche Posted messages 287 Status Membre -
Hello,
I would like to know if it's possible to do an echo in a <script />
Let me explain, I retrieved a code to create a chart.
It has two percentages that add up to 100%.
Currently, these values are fixed in the script, and my goal is to be able to do an "echo" in the script with the previously calculated values. The calculations are no problem!
But the question is how can I write these results in place of the current data?
The code:
It's at the line "data: [50, 50]," where I would like to do something like:
For example... I don't know the limits of JS at all..
Thank you in advance
Configuration: Windows / Opera 70.0.3728.119
Best regards,
Roche Corentin
I would like to know if it's possible to do an echo in a <script />
Let me explain, I retrieved a code to create a chart.
It has two percentages that add up to 100%.
Currently, these values are fixed in the script, and my goal is to be able to do an "echo" in the script with the previously calculated values. The calculations are no problem!
But the question is how can I write these results in place of the current data?
The code:
try { // Percent Chart 2 var ctx = document.getElementById("percent-chart2"); if (ctx) { ctx.height = 209; var myChart = new Chart(ctx, { type: 'doughnut', data: { datasets: [ { label: "My First dataset", data: [50, 50], backgroundColor: [ '#00b5e9', '#fa4251' ], hoverBackgroundColor: [ '#00b5e9', '#fa4251' ], borderWidth: [ 0, 0 ], hoverBorderColor: [ 'transparent', 'transparent' ] } ], labels: [ 'Products', 'Services' ] }, options: { maintainAspectRatio: false, responsive: true, cutoutPercentage: 87, animation: { animateScale: true, animateRotate: true }, legend: { display: false, position: 'bottom', labels: { fontSize: 14, fontFamily: "Poppins,sans-serif" } }, tooltips: { titleFontFamily: "Poppins", xPadding: 15, yPadding: 10, caretPadding: 0, bodyFontSize: 16, } } }); } } catch (error) { console.log(error); } It's at the line "data: [50, 50]," where I would like to do something like:
data: [<?php echo $var1; ?>, <?php echo $var2; ?>],
For example... I don't know the limits of JS at all..
Thank you in advance
Configuration: Windows / Opera 70.0.3728.119
Best regards,
Roche Corentin
1 réponse
PHP generates the page code (HTML, CSS, JS, ...)
So there's no reason why an "echo" in your code at the position of the JS code wouldn't allow you to put that into the JS code.
--
I mainly work in VB6 and VB.NET, with a bit of C#, but moderation often brings me to other languages.
In VB.NET, be sure to enable "Option Explicit" and "Option Strict"
So there's no reason why an "echo" in your code at the position of the JS code wouldn't allow you to put that into the JS code.
--
I mainly work in VB6 and VB.NET, with a bit of C#, but moderation often brings me to other languages.
In VB.NET, be sure to enable "Option Explicit" and "Option Strict"
Just if I move my JS code to a separate file... like
(At the end of my index.php file)
And if at the beginning of my index.php file I define the variables used in test.js.
Will it work or am I forced to include my JS code like this:
Thanks!
No worries about putting PHP code like you do in JS.
However, you need to create your data variable outside of a .js file (since PHP only executes in files with .php or .phtml extensions... unless you configure it in Apache... but I wouldn't recommend that...).
I would do it in your index.php file
And in your .js file
I correctly replaced this with this: in my file js\graphique-pro-part.js
and in my php file, I have:
Thanks again