Reputation: 25
I have to call different JavaScript files depending on the users choice from a dropdown menu.
I tried to do it like this:
if ($subject == "Chemistry") {
'<script src="ChartJS_Chemistry></script>';
}
But that doesn't seem to work.
Any advice on how to activate the right file depending on the dropdown menu choice?
Upvotes: 0
Views: 1016
Reputation: 179
if ($subject == "Chemistry") {
//close php tag here
?>
<script src="ChartJS_Chemistry"></script>
<?php //start php tag again here
}
Upvotes: 0
Reputation: 51
your file path is broken, if your file is ChartJS_Chemistry.js
Call like.
if ($subject == "Chemistry") {
echo "<script src='ChartJS_Chemistry.js'></script>";
}
Upvotes: 0
Reputation: 2984
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.
Upvotes: 3