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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度