Chartjs-How to call a JavaScript file inside a PHP if/else statement?

3👍

As commented, you did not echo anything out.

But also your syntax is wrong:

'<script src="ChartJS_Chemistry></script>';

You’re missing " and I am assuming you’re also missing a file extension?

It should be something like:

echo '<script src="ChartJS_Chemistry.js"></script>';

Also; this is not best practice. Close your PHP tag then write the script tag in HTML:

<?php if( true ) { ?>
  <script src="ChartJS_Chemistry.js"></script>
<?php } ?>

Since then you’r not mixing your PHP with HTML – not good practice.

0👍

your file path is broken, if your file is ChartJS_Chemistry.js

Call like.

if ($subject == "Chemistry") {
    echo "<script src='ChartJS_Chemistry.js'></script>";
}

0👍

if ($subject == "Chemistry") {
        //close php tag here 
?> 
   <script src="ChartJS_Chemistry"></script>
<?php //start php tag again here
}

Leave a comment