简体   繁体   English

如何将PHP变量传递给Javascript函数

[英]How to pass PHP variables to a Javascript function

I don't understand why the addRow method isn't reading the parameters and I know for a fact they are initialized. 我不明白为什么addRow方法不读取参数,而且我知道它们已初始化。

In php 在PHP中
echo "<script> addRow(".$song.",".$artist."); </script>";

JavaScript method JavaScript方法

function addRow(s,a){
        document.write("test");
    }

Don't forget your quotes! 不要忘记您的报价!

echo "<script> addRow( '".$song."', '".$artist."' ); </script>";

Or nicer even: 甚至更好:

echo "<script> addRow( '{$song}', '{$artist}' ); </script>";

PHP PHP

<script> addRow('".$song."','".$artist."'); </script>

JavaScript JavaScript的

function addRow(s,a){
    console.log(s);
    console.log(a);
    document.write("test");
}

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

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