Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I insert PHP and javascript code in the same HTML file?

Tags:

javascript

php

<HTML>
    <script language="JavaScript">  
        <script language="php">
        </script>
    </script>
</HTML>

<HTML>
    <script language="php">
    </script>
    <script language="JavaScript">  
    </script>
</HTML>

I want to insert PHP and javascript code in HTML code like above. Can I do this work??

like image 716
Jimmy Avatar asked Dec 09 '22 19:12

Jimmy


1 Answers

It doesn't work like that, you can have them in the same file per se, just not like you have.

PHP is executed on the server and the result is sent to the client, whereas the JS code is executed by the client's browser.

<?php
//php code in here is evaluated and the result sent to the client
$somevar = 1234;
?>
<HTML>
     <script language="JavaScript">  
        //javascript in here is evaluated by the client
        //you could insert PHP values here to be used in JS if you want
        //make sure you escape them though...
        var some_js_var = <?php echo $somevar; ?>
        //the JS var above would contain the value of php variable $somevar
    </script>
</HTML>
like image 85
shannonman Avatar answered Dec 11 '22 09:12

shannonman