我可以在同一个 HTML 文件中插入 PHP 和 javascript 代码吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13787028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:47:19  来源:igfitidea点击:

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

phpjavascript

提问by Jimmy

<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 代码中插入 PHP 和 javascript 代码。我可以做这个工作吗??

回答by shannonman

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在服务器端执行,结果发送给客户端,而JS代码在客户端的浏览器中执行。

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

回答by Vishnu

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

但是当然,正如其他人指出的那样,浏览器不会看到您的 PHP 代码。它将由服务器处理,浏览器只会看到 javascript/html。

回答by Vishnu

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>

回答by Eugeny89

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.

当然,你甚至可以让 php 生成 javascript。php 文件在发送给客户端之前被处理为 html,客户端除了 html 和 javascript 什么都看不到。

回答by RKINFOPHP

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

你可以把 PHP 代码放在 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.

许多人不知道,但原则上不知道。如果服务器解释php文件需要查看php扩展名,注意不能设置或者如果文件是php html扩展名,那么你就不是js文件扩展名。你可以做的是类似于 php type="javaScript"> '; echo 'write("'.$name.'");'; 回声''; } ?> 例如。

回答by developer

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

如果你想让 php 在 javascript 中执行,那么你必须使用 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).

AJAX 是一个 javascript 代码,它允许您调用服务器,从而在您希望的任何时间执行 php 代码并将结果返回给您(不仅在页面创建时,而且在调用 javascript 代码时) .

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

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

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

使用 jquery 更容易:https: //api.jquery.com/jquery.ajax/