Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

share|improve this question

closed as unclear what you're asking by karthik, JoseK, Marcin Nabiałek, Dipak Ingole, benka Sep 10 '14 at 9:07

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
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 8:16
    
you can have your answer here 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 8:33
    
possible duplicate of Use PHP code in external Javascript file – Synchro Sep 10 '14 at 8:51
up vote 0 down vote accepted

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.

share|improve this answer
    
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 8:54

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

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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>
share|improve this answer
    
Use AJAX? That is unnecessary and you are not even showing it. – putvande Sep 10 '14 at 8:21
    
True. This is Cookie. My mistake. In that case, take the cookie by js. – scalloty Sep 10 '14 at 8:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.