-5

Possible Duplicate:
Parse a JavaScript file through PHP
PHP no longer working inside javascript

I am running .php files on my localhost and yesterday they worked and today they have errors, but I haven't changed the code. Here is a test file that has the same error as my longer actual code:

<html>
  <script type="text/javascript">
    <? $message = "Hello"; ?>
  </script>
  <? echo $message; ?>
</html>

I know this is a silly way to write Hello, but it demonstrates what error I am getting when I try to use php inside javascript. It was working perfectly fine yesterday and none of the code has changed. When I run this, it says "unexpected token <" on line 3. I think it is a problem with apache/php/mysql but I'm lost trying to fix it. Any suggestions would be great. Thanks.

Community
  • 1
  • 1
Kristi
  • 21
  • 1
  • 1
  • 4
  • 4
    Probably your web server doesn't support PHP short tags. Have you tried using ` – Zeta Jan 31 '13 at 17:24
  • That's not the job of javascript to parse PHP. It should be interpreted (and removed) server side. – Denys Séguret Jan 31 '13 at 17:25
  • i knew they where gonna downvote. everyone in this forum need to be graduated or above to make a question, or youre gonna get downvoted to hell. – Toping Jan 31 '13 at 17:27
  • @Ark More like, "you need to learn before coming and posting your problem (which doesn't make sense) over and over again" – Some Guy Jan 31 '13 at 17:28
  • its not clear what he is trying to do. tbh – VeXii Jan 31 '13 at 17:29
  • the guy is going negative – Toping Jan 31 '13 at 17:34
  • Perhaps learn something about [Client Side](http://en.wikipedia.org/wiki/Client_side) (JavaScript) versus [Server Side](http://en.wikipedia.org/wiki/Server_side) (PHP) – Ryan Kinal Jan 31 '13 at 17:34
  • Have you tried changing your shorttags as several people suggested? – j08691 Jan 31 '13 at 17:36
  • 2
    @PraveenKumar You should have let one of your answers longer, in my opinion. You might be right and OP should look at this. – Denys Séguret Jan 31 '13 at 17:36
  • @dystroy Some idiots troll seriously by downvoting. The main reason is that I have a good reputation and I don't want that to get spoilt, even though my intention is genuinely to help people. AFAIC, there should be no downvote. What say? Anyways, others have answered it. So, mission accomplished. :) – Praveen Kumar Purushothaman Jan 31 '13 at 17:39
  • 1
    @PraveenKumar Somebody downvoted you, with or without a reason (I can say if he's a idiot or if he found a problem in your question). That's not a big deal. You should wait to see if you solve the problem. And if you care so much about those points you can always come back tomorrow to remove the answer. – Denys Séguret Jan 31 '13 at 17:40
  • @dystroy As you said. :) – Praveen Kumar Purushothaman Jan 31 '13 at 17:41
  • 1
    I changed my short tags and it worked! Now I'm trying to find /private/etc/php.ini on my mac and enable short tags. I'm so confused as to why the short tags worked yesterday but didn't today, but oh well it worked! Thank you. – Kristi Jan 31 '13 at 17:43

3 Answers3

1

I guess, you have short tags disabled. You need to enable PHP short tags in order for this to work. Check if this works:

<html>
  <script type="text/javascript">
    <?php $message = "Hello"; ?>
  </script>
  <?php echo $message; ?>
</html>

And in my opinion, I have no clue, why you open a <script> tag and declare a PHP Server Side statement there. You know what? It doesn't output anything. Anyways, the output of the above code would be this:

<html>
  <script type="text/javascript">

  </script>
  Hello
</html>

So there's no need of having the <script> tag as it is client side and PHP doesn't care about that. You can just give it anywhere you want, and as long as they are inside <?php ?> tags, they won't be parsed by the browser. :)


If you are doing this in your own system, to enable short tags in PHP, do the following:

  1. Locate the php.ini file.
  2. Find this:

    short_open_tag = Off
    
  3. Change it to:

    short_open_tag = On
    

From the manual:

short_open_tag: Tells PHP whether the short form (<? ?>) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"?>'; ?>. Also, if disabled, you must use the long form of the PHP open tag (<?php ?>).

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

its not valid javascript.

after the php is run you end up with a dom looking like <script type="text/javascript"></script> because you're not writing anything.

try:

<script>
var hello = "<?php echo 'hello' ?>"
</script>
<script type="text/javascript">
document.writeln(hello)
</script>
Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
VeXii
  • 3,079
  • 1
  • 19
  • 25
-1

The script as it is works fine for me. Is the code posted in your question complete? The only way I was able to reproduce the problem was with the following code:

<?php
<html>
  <script type="text/javascript">
    <? $message = "Hello"; ?>
  </script>
  <? echo $message; ?>
</html>
?>

Correct me if I'm wrong, but I'm assuming the code you posted is in a *.php file and in between php tags.. If this is the case, remove said tags and it should work.

phex
  • 104
  • 3
  • 10
  • Not sure why this is being downvoted.. shouldn't we be considering all possible problem sources? – phex Jan 31 '13 at 17:40
  • 1
    Dude, I didn't downvote. But, just understand what is happening, although your answer doesn't help! Actually to be frank, your answer further screws up the code. You have `short_open_tag` enabled and the OP doesn't. Read more about it and you will know why you have been downvoted. – Praveen Kumar Purushothaman Jan 31 '13 at 17:43
  • @PraveenKumar No worries, I wasn't really complaining ;-) I just though that _if_ OP did in fact have `short_open_tag` enabled, this could likely be a solution. – phex Jan 31 '13 at 17:47
  • Not likely, that is THE SOLUTION. :) – Praveen Kumar Purushothaman Jan 31 '13 at 17:48
  • Nono, I meant the other way around: assuming OP had `short_open_tag` enabled and was still experiencing the problem, my answer could have been a likely candidate :) – phex Jan 31 '13 at 17:50
  • How do you think that would even be a reason? – Praveen Kumar Purushothaman Jan 31 '13 at 17:52
  • 1
    It was just an assumption based on common mistakes I encountered when I first started with PHP. I agree that I shouldn't have mentioned the above being "the only way" ;) – phex Jan 31 '13 at 17:59