简体   繁体   English

从php传递编码变量并在javascript中解码?

[英]Passing encoded variable from php and decoding in javascript?

I have a very ugly piece of code in php that does something like this: 我在php中有一段非常丑陋的代码,它执行以下操作:

for ($i = 0; $i <= ($fi_int-1); $i++) {
        $song_uri = urlencode($songs[$i]);

        echo "URI: " . $song_uri . "<br>";
        echo "<li><a href='#' onclick='PlaySong(".$song_uri.")'>" . $songs[$i] . "</li><br>";
    } 

Now when PlaySong() is being called, this happens: 现在,在调用PlaySong()时,会发生以下情况:

<script>

            function PlaySong(title) {
                title = decodeURIcomponent(title));
                document.getElementById("player").src = "./mp3/" + title;
            }

</script>

The current problem is when $songs[$i] is being passed to PlaySong(), it looks like that right away: 当前的问题是,当$ songs [$ i]被传递给PlaySong()时,它看起来像这样:

PlaySong(Name+Of+The+Song+-+Artist+LastName+blah) {
  ...
}

So JS obviously has a problem with it. 因此,JS显然存在问题。 It tries to add things, because there are pluses... Now how can I convert that mess to string right when it comes in? 它试图添加东西,因为有加号...现在,当出现混乱时,如何将其正确转换为字符串? Or is there a better way to do it? 还是有更好的方法呢? I'm sure there is but this ugliness is strictly for me, so I don't care too much about fast performance :) 我敢肯定有,但是这种丑陋严格来说是我的,所以我不太在乎快速的性能:)

You need some quotes around the song_uri string. 您需要在song_uri字符串周围加上一些引号。 I can't paste code on mobile... but the php should output like this 我无法在手机上粘贴代码...但是php应该这样输出

Onclick="PlaySong ('this+song')" Onclick =“ PlaySong('this + song')”

At the moment you've got it doing 目前,您已经做好了

Onclick="PlaySong(this+song)" and the argument isn't being treated as a string Onclick =“ PlaySong(this + song)”并且该参数未被视为字符串

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

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