Echo in a JS script

Solved
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:

 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

NHenry Posted messages 2510 Registration date   Status Modérateur Last intervention   385
 
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"
1
CorentinRoche Posted messages 287 Status Membre 41
 
Ok, actually it works well ... it's an error in another part of my code, sorry!

Just if I move my JS code to a separate file... like
<script src="js/test.js"></script>

(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:
<script>the script</script>


Thanks!
0
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830 > CorentinRoche Posted messages 287 Status Membre
 
Hello,

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
 <script> var datas = [<?php echo $var1 ;?>, <?php echo $var2;?>] ; </script> 


And in your .js file
 data: datas, 
0
CorentinRoche Posted messages 287 Status Membre 41 > jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention  
 
I just took the test, it didn't work...

I correctly replaced this
data: [<?php echo $var1 ;?>, <?php echo $var2;?>],
with this:
data: datas,
in my file js\graphique-pro-part.js

and in my php file, I have:
 <script src="js/graphique-pro-part.js"></script> <script> var datas = [<?php echo $pourcentage_de_client_PRO ;?>, <?php echo $pourcentage_de_client_PART ;?>]; </script>
0
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830 > CorentinRoche Posted messages 287 Status Membre
 
A web page is read from top to bottom by the browser... if you place the initialization of your variable after loading your js, it won't work.
0
CorentinRoche Posted messages 287 Status Membre 41 > jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention  
 
Indeed ... Basic mistake, sorry!

Thanks again
0