0

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?

1
  • 2
    You forgot echo? Commented Jun 14, 2019 at 21:13

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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>";
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.