Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm not sure if its possible or if there is another way to do this but I've been trying to loop a JavaScript function with PHP.

THis is the Javascrip/PHP Function:

<script type="text/javascript">
<?php
while ($songsFunction = mysql_fetch_array($result3)) {

echo "function ".$songsFunction['songID']."(){";
echo "document.getElementById('ogg').src='';";
echo "document.getElementById('mp3').src='';";
echo "document.getElementById('songName').innerHTML='';";
echo "audio.load();";
echo "playPause();";
}
?>
</script>

I have the songID showing up on the playlist:

<?php while ($haha = mysql_fetch_array($result3)) {
echo   '<ul>
        <li>
        <a href="#" onClick="'.$songFunction['songID'].'();">'.$songFunction['songName'].'</a>
        <a href="'.$songFunction['mp3Link'].'"><img src="../images/download.png"></a>
        </li>
        </ul>';  
   }
    ?>

The playlist correctly loops. But when i get the function working, if the songID's match up. The playlist disappears...

Is this the correct way to loop a JavaScript function whilst pulling information through a database with variables?

share|improve this question
1  
I don't think generating several JavaScript functions is what you want. Just pass in what parameters you need to a single JavaScript function. – scunliffe Oct 23 '12 at 13:05
1  
if $songFunction['songID'] is a number, prefix the output with a non-number - function names cannot start with number. – pozs Oct 23 '12 at 13:10
Sorry for the messy/unorganized code. Currently just trying to get it to work before i make everything valid/organized. – user1719526 Oct 23 '12 at 13:13

3 Answers

You are not closing your JavaScript function.

share|improve this answer

I'm not sure exactly what you are trying to accomplish but the only JavaScript I see manipulating the DOM just erases the content...

echo "document.getElementById('ogg').src='';";
echo "document.getElementById('mp3').src='';";
echo "document.getElementById('songName').innerHTML='';";

thus whatever content these items used to have would be erased. Is this what you intended? or the source of the problem?

share|improve this answer
I know that they are erasing whats there. I don't need them to have anything in them as of now. I'm just experiencing issues with actually looping the playlist and Function correctly. – user1719526 Oct 23 '12 at 13:17

This is definitely NOT the way to do it.

The scenario should be:

  1. User clicks a song from playlist.
  2. A click event is triggered, sending an ajax request to the php script.
  3. The PHP script pulls data from DB and returns a response formatted as json
  4. js code uses the json response and do something with it.
share|improve this answer
just curious - why is ajax needed? a simple URL to the filename is surely all that is needed and already provided, no? – scunliffe Oct 23 '12 at 13:18
1  
^ Ajax really shouldn't be required for this. – user1719526 Oct 23 '12 at 13:20
The main thing you should get from this, is you should separate js and PHP code. – ftom2 Oct 23 '12 at 14:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.