简体   繁体   English

从 JavaScript 回显 PHP 变量?

[英]Echo PHP variable from JavaScript?

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!";
    ?>

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";?>');

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.您应该能够检查仅查看代码生成的 JS。 Of course, if the JS relies on some PHP variables, they need to be instanciated before the JS is output.当然,如果 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将不会

Use json_encode to convert some text (or any other datatype) to a JavaScript literal.使用json_encode将一些文本(或任何其他数据类型)转换为 JavaScript 文字。 Don't just 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.最好的情况是您的代码失败,最坏的情况是您有一个很大的旧跨站点脚本安全漏洞。

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>

Your using @$_POST indicates that you have received (or are expecting) errors - check your generated source to see if the value was output correctly.您使用@$_POST表示您收到(或预期)错误 - 检查您生成的源以查看值是否正确输出。 Otherwise document.getElementById will fail and you'd get no output.否则document.getElementById将失败,您将得不到任何输出。

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

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.原因是,php 解析它应该解析的文件。 The file types that php will parse are configured in httpd.conf using AddType commands (or directives, whatever they are called). php 将解析的文件类型使用 AddType 命令(或指令,无论它们被调用)在 httpd.conf 中配置。

So you have 3 options:所以你有3个选择:

  • add filetype js to the list of files php will parse (BAD, VERY BAD)将文件类型 js 添加到 php 将解析的文件列表中(糟糕,非常糟糕)
  • make the script inline to some php file使脚本内联到一些 php 文件
  • rename the file to script.js.php, and at the beginning of the file, specify the content type, like so:将文件重命名为script.js.php,并在文件开头指定内容类型,如下所示:

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

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM