douyinbo3361 2016-06-26 06:00
浏览 53
已采纳

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

I have a very ugly piece of code in php that does something like this:

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:

<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:

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

So JS obviously has a problem with it. 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 :)

  • 写回答

1条回答 默认 最新

  • doumang20060820 2016-06-26 06:30
    关注

    You need some quotes around the song_uri string. I can't paste code on mobile... but the php should output like this

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题