最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论
<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??

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

Share Improve this question asked Dec 9, 2012 at 11:36 JimmyJimmy 4735 gold badges9 silver badges13 bronze badges 4
  • you cant do this in php as it really does not make any sense to add some php script in html like this please clarify your question – Hemen Ashodia Commented Dec 9, 2012 at 11:39
  • Sure you can. Just replace those javascript type of tags with <?php. As long as the server or the browser (depending upon what language it is) can process, you can put whatever you want (know as spaghetti code), but you shouldn't. – itachi Commented Dec 9, 2012 at 11:46
  • 2 PHP <script language=“php”> tags might helps. – Muhammad Saifuddin Commented Dec 9, 2012 at 11:52
  • You can generally make the second HTML block work if you configure your webserver to understand the php script tag as PHP code. But what you cannot do is insert any tags inside a <script> tag according to the HTML definition. – crackmigg Commented Dec 9, 2012 at 11:56
Add a comment  | 

6 Answers 6

Reset to default 10

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>
<HTML>
<script language="JavaScript">  
    <?php 
      // Your PHP code here that outputs javascript
      ?>
</script>
</HTML>

<HTML>
    <?php 
      // Your PHP code here that outputs text/html code
      ?>
<script language="JavaScript">  
</script>
</HTML>

But of course, as others pointed out, browser will not see your PHP code. It will be processed by the server and browser will see only the javascript/html.

sure, you can even make php generate javascript. php file is processed to html before sending it to client, and client will see nothing except html with javascript.

You can also insert your code like this, don't give error

<HTML>
    <script language="JavaScript">  
        <script language="php">
           echo "alert('javascript alert')";
        </script>
    </script>
</HTML>

<HTML>
    <script language="php">
         echo "php code runned";
    </script>
    <script language="JavaScript">  
    </script>
</HTML>

If you want php to be executed inside javascript, then you have to use AJAX.

AJAX is a javascript code that allows you to call the server, thus executing php code and returning the result to you, at any time you wish (not only at the creation time of the page, but at the time the javascript code is called).

https://www.w3schools.com/xml/ajax_intro.asp

with jquery is even easier: https://api.jquery.com/jquery.ajax/

You can put PHP code in the middle of a fichero.js???

Many do not know, but not in principle. If the server to interpret a php file needs to see the php extension, note that you can not set or if the file is php html extension, therefore you're not a js file extension. What you can do is something like the php type="javaScript"> '; echo 'write ("'. $ name. '");'; echo ''; } ?> For example.

发布评论

评论列表(0)

  1. 暂无评论