-6

So in my php page I have this code :

<div id="test"></div>
<script src="test.js"></script>

And in my external javascript I have :

document.getElementById('test').innerHTML=
"<div id='abc'><a href='index.html'><?php echo 'Welcome '.$_COOKIE['user'].'<br>' ;?></a></div>";

Cookie has been set and if I put the script inside the php page, it does work but why isn't it working when it is external script ? Did I do something wrong here ? or is there a rule for doing this ?

Please educate me. Thx in advance. =D

Vantablack
  • 45
  • 1
  • 9
  • A JavaScript file can't have PHP in it. Unless you tell the server it needs to render it as PHP. – putvande Sep 10 '14 at 08:16
  • you can have your answer here http://stackoverflow.com/a/9083465/3808383 Please make a habit of searching on your own. There are lots of answered questions quite similar to your question. – subas_poudel Sep 10 '14 at 08:33
  • possible duplicate of [Use PHP code in external Javascript file](http://stackoverflow.com/questions/9083089/use-php-code-in-external-javascript-file) – Synchro Sep 10 '14 at 08:51

5 Answers5

0

You can't use PHP in a javascript file like that. They are two totally different languages.

Stephan Muller
  • 27,018
  • 16
  • 85
  • 126
0

You can't render php on a Javascript file, but you can do this:

on your php file:

<script>
var cookie = "<?php echo 'Welcome '  . $_COOKIE['user'] . '<br>'; ?>";
</script>
<div id="test"></div>

and in your js file:

document.getElmentById('test').innerHTML = cookie;

Regards

Manjar
  • 3,159
  • 32
  • 44
0

While using PHP and javascript together is absolutely possible, you have to remember, that PHP is executed on server-side while javascript is executed on client-side.

So you cannot have PHP in your javascript, as the page lifecycle is like this:

  • Get the request from client browser
  • Execute the PHP of the requested page and provide the html
  • Send the html to the client browser
  • Render the html in the client browser
  • Execute the javascript of the page

So after the page is sent, the php processing is done.

You can however embed variable via php for later use with javascript. If you echo out something like this, your java scripts can use it:

<script type="text/javascript">
  var myCookie = "<?php echo $_COOKIE['user'];?>";
</script>

If you do a lot of stuff with cookies in your javascript, you may even better have a look at jQuery Cookies and fetch the cookie value directly. This will make you end up with much cleaner code.

Kai Mattern
  • 3,090
  • 2
  • 34
  • 37
  • Thx for your clear explanation, it give me a better picture of javascript and php. I am trying to learn jquery too. – Vantablack Sep 10 '14 at 08:54
0

We cannot add php code inside the js file.

PHP is server side scripting language which will executed in the server and response will returned to the browser, while Javascript is client side scripting language which is executed in the browser.

Varun Victor
  • 100
  • 5
-1

This is a solution but just use ajax to get the required information from the server.

<div id="dom-target" style="display: none;">
    <?php 
        $output = "42"; //Again, do some operation, get the output.
        echo htmlspecialchars($output); /* You have to escape because the result
                                       will not be valid HTML otherwise. */
    ?>
</div>
<script>
   var div = document.getElementById("dom-target");
   var myData = div.textContent;
</script>
scalloty
  • 108
  • 5