从 JavaScript 回显 PHP 变量?

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

Echo PHP variable from JavaScript?

phpjavascripthtml

提问by micahwittman

I have a PHP page with some JavaScript code also, but this JavaScript code below doesn't seem to work, or maybe I'm way off!

我也有一个带有一些 JavaScript 代码的 PHP 页面,但是下面的这段 JavaScript 代码似乎不起作用,或者我可能离题了!

I am trying something like this:

我正在尝试这样的事情:

  var areaOption=document.getElementById("<?php echo @$_POST['annonsera_name']?>");
  areaOption.selected=true;

Also I have tried this, but it only alerts a BLANK alert-box:

我也试过这个,但它只提醒一个空白的警报框:

    alert (<?php echo $test;?>); // I have tried this with quotes, double-quotes, etc... no luck

Am I thinking completely wrong here?

我在这里思考完全错误吗?

UPDATE

更新

Some PHP code:

一些PHP代码:

    <?php 
        $test = "Hello World!";
    ?>

回答by micahwittman

In your second example, you are missing quotes around the string (so H is interpreted as a variable - which you didn't set).

在你的第二个例子中,你在字符串周围缺少引号(所以 H 被解释为一个变量 - 你没有设置)。

Test this:

测试这个:

alert (<?php echo "'H'";?>);

OR

或者

alert ('<?php echo "H";?>');

回答by Benoit Vidis

PHP runs on the server side and Javascript is running on the client side.

PHP 在服务器端运行,Javascript 在客户端运行。

The process is that PHP generates the Javascript that will be executed on the client side.

过程是PHP生成将在客户端执行的Javascript。

You should be able to check the JS that is generated just looking at the code. Of course, if the JS relies on some PHP variables, they need to be instanciated beforethe JS is output.

您应该能够检查仅查看代码生成的 JS。当然,如果 JS 依赖于一些 PHP 变量,则需要在 JS 输出之前对其进行实例化。

<?php
$test = 'Hello world';
?>
<html>
    <body>
        <script>
            alert('<?php echo $test; ?>');
        </script>
    </body>
</html>

will work but

会工作,但是

<html>
    <body>
        <script>
            alert('<?php echo $test; ?>');
        </script>
    </body>
</html>
<?php
$test = 'Hello world';
?>

will not

将不会

回答by bobince

Use json_encodeto convert some text (or any other datatype) to a JavaScript literal. Don'tjust put quotes around the echoed string?—?what if the string has a quote in it, or a newline, or backslash? Best case your code fails, worst case you've got a big old cross-site-scripting security hole.

使用json_encode将一些文本(或任何其他数据类型)转换为 JavaScript 文字。不要只在回显的字符串周围加上引号?—?如果字符串中有引号、换行符或反斜杠怎么办?最好的情况是您的代码失败,最坏的情况是您有一个很大的旧跨站点脚本安全漏洞。

So,

所以,

<?php
    function js($o) {
        echo json_encode($o, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP);
    }
?>
<script type="text/javascript">
    var areaOption= document.getElementById(<?php js($_POST['annonsera_name']); ?>);
    areaOption.selected= true;
    alert (<?php js('Hello World'); ?>);
</script>

回答by jrharshath

If your extension is js, php will not work in that file.

如果您的扩展名是 js,则 php 将无法在该文件中运行。

The reason being, php parses on files that it is supposed to. The file types that php will parse are configured in httpd.conf using AddType commands (or directives, whatever they are called).

原因是,php 解析它应该解析的文件。php 将解析的文件类型在 httpd.conf 中使用 AddType 命令(或指令,无论它们被调用)进行配置。

So you have 3 options:

所以你有3个选择:

  • add filetype js to the list of files php will parse (BAD, VERY BAD)
  • make the script inline to some php file
  • rename the file to script.js.php, and at the beginning of the file, specify the content type, like so:

    <?php header( 'content-type: text/javascript' ); ?>

  • 将文件类型 js 添加到 php 将解析的文件列表中(糟糕,非常糟糕)
  • 使脚本内联到一些 php 文件
  • 将文件重命名为script.js.php,并在文件开头指定内容类型,如下所示:

    <?php header( 'content-type: text/javascript' ); ?>

Cheers!

干杯!

回答by K Prime

Your using @$_POSTindicates that you have received (or are expecting) errors - check your generated source to see if the value was output correctly. Otherwise document.getElementByIdwill fail and you'd get no output.

您的使用@$_POST表明您已收到(或预期)错误 - 检查您生成的源以查看该值是否正确输出。否则document.getElementById会失败,你不会得到任何输出。

回答by streetparade

 alert("Delete entry <? echo $row['id']; ?> ")