-1

I have a PHP 'if' that only loads a certain JavaScript if it is applicable:

(this is from the <head> of my page):

<?PHP if ($row['jstype'] == 'myanswer')
  {
  echo '<script type="text/javascript" src="js/index.js"></script>';
  }
else
  {
  echo '';
 }
 ?>

Now, I have checked the source on a page that it applies to and the js appears in the html. However, it is not loading. I can only assume that this is because it is not loading with enough time after getting it's instruction from the PHP. Is this a correct assumption?

And, more importantly, can anyone suggest a way of doing what I need it to do?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1259798
  • 171
  • 1
  • 16
  • 3
    "I can only assume that this is because it is not loading with enough time after getting it's instruction from the PHP. Is this a correct assumption?" — No. How are you determining that it is not loading? Have you checked the browser's developer tool's Net tab to see what HTTP response you are getting? – Quentin Aug 09 '13 at 09:25
  • 3
    You can drop the else part entirely. – ciruvan Aug 09 '13 at 09:25
  • 1
    @deceze — No. There is no attempt to pass data between PHP and JS in the question. – Quentin Aug 09 '13 at 09:26
  • no. That's not a correct assumption because PHP is parsed before the HTML is going to be delivered. There's only the possibility that there's something wrong in your js. – thpl Aug 09 '13 at 09:26
  • 2
    @Quentin But there's a clear misunderstanding about the interaction between PHP and Javascript, which that question attempts to clear up in general. – deceze Aug 09 '13 at 09:26
  • 1
    @deceze — Not one covered by that question. – Quentin Aug 09 '13 at 09:27
  • 1
    Please look in the [JavaScript error console](http://www.netmagazine.com/tutorials/javascript-debugging-beginners) and tell what errors you see there. – JJJ Aug 09 '13 at 09:29
  • A continuation of the problem - it turns out that the js doesn't work in the head (only in the bottom of the page, perhaps a loading problem) but the PHP doesn't seem to work at the bottom (only at the head). Confused much?! – user1259798 Aug 09 '13 at 13:46

1 Answers1

1

set up a test file index.js containing only:

alert('boo!');

Test that works with a hard-coded version of your js include line.

Prove it works.

Then add your PHP version.

PHP spits out the whole stream to the browser, so no, its nothing to do with PHP - probably something to do with relative/absolute paths in the SRC of the js defn - hence the incremental development idea above should help identify where you are going wrong. Divide and conquer.

The code you showed means there is no point in having an else, so you could shorten it to:

<?php
if ($row['jstype'] == 'myanswer')
  echo '<script type="text/javascript" src="js/index.js"></script>';
?>

Depending on how many ELSE conditions you actually do need, show them and its possible someone can suggest better ways of writing it.

Cups
  • 6,901
  • 3
  • 26
  • 30
  • Thanks for your response, @Cups. I have had the js working in a hardcoded setting, but just not using the PHP. (Also, the ELSE condition details other solutions that are going to be incorporated as the development continues, but - as you say - aren't applicable here). – user1259798 Aug 09 '13 at 09:40
  • Glad I helped. When you have 2 or 3 other else conditions (which seemed to be a sub-question of yours) that you can show in code then ask another question and you should get some alternatives to using if/elseif/elseif/else blocks. Try and apply the DRY prinicple so that you only write the '; block once. – Cups Aug 09 '13 at 13:29
  • Can you think of a reason why this particular chunk of PHP wouldn't work if inserted at the bottom of the page? The problem seems to be that the js doesn't work in the head - it only works if loaded last on the page - but when I run the PHP script at the bottom of the page, it doesn't render - nothing appears in the html output. Clues? – user1259798 Aug 09 '13 at 13:53
  • Actually, don't worry - problem solved (error in my code when passing the PHP variable). – user1259798 Aug 09 '13 at 14:16