在 PHP 中使用 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17796330/
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-27 09:39:44  来源:igfitidea点击:

using javascript in PHP

phpjavascript

提问by

i have this php and js code:

我有这个 php 和 js 代码:

<script type="text/javascript">
function StartScript(script)
{
  <?php require("StartScript.php");  ?>
}
</script>

<a href="#" onclick="StartScript(scriptname);">Start</a>

How can i get the "script" from the javascript function onto the end of the php file?

如何从 javascript 函数中获取“脚本”到 php 文件的末尾?

for example rather than:

例如而不是:

<?php require("StartScript.php");  ?>

to have:

具有:

<?php require("StartScript.php?script=scriptname");  ?>

采纳答案by David Riccitelli

The full excerpt (provided that StartScript.phpat the same level of current page, otherwise add absolute path):

全文摘录(前提是StartScript.php当前页面同级,否则添加绝对路径):

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

<script type="text/javascript">
   function StartScript(script) {
       jQuery.getScript('StartScript.php?script=' + escape(script));
    }
</script>

This will call StartScript.phpwith the parameter script. StartScript.phpwill generate the JavaScript code which will be executed in the client browser.

这将StartScript.php使用参数调用scriptStartScript.php将生成将在客户端浏览器中执行的 JavaScript 代码。

Reference: http://api.jquery.com/jQuery.getScript/

参考:http: //api.jquery.com/jQuery.getScript/

回答by Pieter

It's impossible to write server-side code (PHP) with a client-side code (Javascript). This will not work unless you use something like Ajax.

用客户端代码 (Javascript) 编写服务器端代码 (PHP) 是不可能的。除非您使用 Ajax 之类的东西,否则这将不起作用。

回答by hendr1x

Maybe I misunderstood your question but this seems pretty easy...I would include all the js in the php file so the html side of things would have only....

也许我误解了您的问题,但这似乎很简单……我会将所有 js 包含在 php 文件中,因此 html 方面只有……

<?
$script = "Page1";
include("StartScript.php");
?>

and the php would be...

而 php 将是...

<? if ($script == "Page1"){ ?>
function StartScript(script)
{
  alert("IN");
}
</script>
<? } ?>

回答by matmar10

The problem is that you're mixing up the client (JS) and the server (PHP). PHP is executed on the server and produces some HTML which gets sent to the browser ("the client"). Browsers cannot run PHP because it's a server-side language.

问题是您混淆了客户端 (JS) 和服务器 (PHP)。PHP 在服务器上执行并生成一些发送到浏览器(“客户端”)的 HTML。浏览器不能运行 PHP,因为它是一种服务器端语言。

You should evaluate what the desired interaction is between the client and the server here. For example, if you just need to execute some PHP to pass data to JavaScript, you can build up a JavaScript object:

您应该在此处评估客户端和服务器之间所需的交互。例如,如果你只需要执行一些 PHP 来向 JavaScript 传递数据,你可以构建一个 JavaScript 对象:

<script type="text/javascript">
    <?php // include script that gives you back some data, e.g.: ?>    
    <?php $somePhpData = array('red', 'yellow', 'blue'); ?>
    <?php $jsonData = json_encode($somePhpData); ?>
    var dataFromPhpScript = <?php echo $jsonData; ?>
    // do something with the data
</script>

Otherwise, if you really need JavaScript to trigger a PHP script running, you're essentially doing AJAX. You'll likely want some sort of REST API. The idea is that you expose a URL from PHP that the JavaScript can call:

否则,如果您确实需要 JavaScript 来触发 PHP 脚本运行,那么您实际上是在使用 AJAX。您可能需要某种 REST API。这个想法是你从 PHP 公开一个 JavaScript 可以调用的 URL:

<script type="text/javascript">    
    jQuery(document).ready(function() {     
        function startScript(scriptName, success) {        
                $.get('/path/StartScript.php?script-name=' + scriptName)
                .done(success)
                .fail(fail);
        }           
        startScript('name-of-php-script', function(data) {
            // trigger some JavaScript that relies on the output of the PHP script
        });         
    });
</script>