echo php导致javascript代码

时间:2013-02-04 20:14:00

标签: php javascript jplayer

我试图在javascript中回应一些PHP中的结果,但它一直没有搞乱javascript播放器的界面

这是完整的javascript:

<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){

    new jPlayerPlaylist({


        jPlayer: "#jquery_jplayer_1",
            cssSelectorAncestor: "#jp_container_1"
        }, [
            {
                title:"Name",
                mp3:"audio.mp3",
            },      

        ], {
            swfPath: "js",
            supplied: "oga, mp3",
            wmode: "window"
        });
    });
    //]]>
    </script>

我想替换它:

            {
                title:"Name",
                mp3:"audio.mp3",
            },      

用这个:

    while(
    $row = mysql_fetch_assoc($result)) { 
    $sender = $row['sender'];
    $sender_name_query = mysql_query("SELECT fullname FROM users WHERE id = '$sender'");
    $sender_name = mysql_fetch_object($sender_name_query);
    $sender_fullname = $sender_name->fullname;
    echo '{<br/>title:"' . $sender_fullname . '",<br/>mp3:"link",<br/>},';  
}   

这是一个循环我需要它来获得所有结果

任何人都可以帮忙解决这个问题吗? 感谢

4 个答案:

答案 0 :(得分:2)

<br/>在javascript中无效。尝试:

echo '{\ntitle:"' . $sender_fullname . '",\nmp3:"link",\n},';  

答案 1 :(得分:2)

又一个解决方案。你可以这样做:

<?php
    $playlist = array();

    while($row = mysql_fetch_assoc($result)) { 
        $sender = $row['sender'];
        $sender_name_query = mysql_query("SELECT fullname FROM users WHERE id = '$sender'");
        $sender_name = mysql_fetch_object($sender_name_query);
        $sender_fullname = $sender_name->fullname;
        $playlist[] = (object) array(
            'title' => $sender_fullname,
            'mp3' => 'audio.mp3'
        );  
    }   
?>

<script type="text/javascript">
    //<![CDATA[
    $(document).ready(function(){
        new jPlayerPlaylist({
            jPlayer: "#jquery_jplayer_1",
            cssSelectorAncestor: "#jp_container_1"
        }, 
        <?php echo(json_encode($playlist));?>,
        {
            swfPath: "js",
            supplied: "oga, mp3",
            wmode: "window"
        });
    });
    //]]>
</script>

答案 2 :(得分:1)

请勿使用<br>代码,而是使用\n来添加换行符(如果您确实需要,则脚本无法使用换行符。)

您无法在javascript中使用HTML标记

答案 3 :(得分:1)

{
    title: "<?php echo json_encode($sender_fullname);?>",
    mp3: "audio.mp3",
},