0

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

2
  • I don't understand the problem. You urlencode the string, so you end up with a urlencoded string. Pluses are spaces in urlencoded strings, so whats wrong with this? Commented Jun 26, 2016 at 6:03
  • @tkausl What exactly is wrong is that it doesn't work. My idea, as I described above why it doesn't work is that when js takes over, inside of PlaySong() function parameter list, there is a crap load of mess which says: Take Name add it to Of add that to The add that to Name and so on. So it doesn't matter that I try to decode later, even before that, in the argument part of the function it already gets confused. But maybe I'm wrong. The fact still stays the same - this scheme doesn't work :( Commented Jun 26, 2016 at 6:20

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Inside of my for loop, I put single quotation marks like this: onclick='PlaySong('".$song_uri."')' but now the chrome console says this: Uncaught SyntaxError: Unexpected token } near here: (function(event){PlaySong( })
Ye that won't work because the second single quote is closing the first. I would swap the single quotes arround onclick to double quotes. You'll need to escape these as they're within the echo so will be something like onclick=\"....
Awesome, with a couple more tweaks I made it work! Thank you so much!
Ye sorry, I was on my mobile unable to get to a PC so couldn't just paste the code. Glad you got it working :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.