-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

3
  • A JavaScript file can't have PHP in it. Unless you tell the server it needs to render it as PHP. Commented Sep 10, 2014 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. Commented Sep 10, 2014 at 8:33
  • possible duplicate of Use PHP code in external Javascript file Commented Sep 10, 2014 at 8:51

5 Answers 5

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.

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

1 Comment

Thx for your clear explanation, it give me a better picture of javascript and php. I am trying to learn jquery too.
0

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

Comments

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

Comments

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.

Comments

-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>

2 Comments

Use AJAX? That is unnecessary and you are not even showing it.
True. This is Cookie. My mistake. In that case, take the cookie by js.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.