当if / elseif被触发时,PHP按钮停止工作

时间:2014-02-25 14:32:56

标签: php jquery button if-statement

我正在尝试回显一个按钮,将默认div中的歌曲添加到播放列表中:

    <?php
        include('dbcon.php');
            if(isset($_REQUEST['all']) && $_REQUEST['all'] != ''){
                    //===============================Button "ALL"====================================
                    unset($_REQUEST['kw']);
                    unset($_REQUEST['genre']);

                    $query = "select * from music";
                    $result = mysqli_query($link, $query) or die (mysqli_error());
                    echo "<ul>";
                    while($info = mysqli_fetch_array( $result )){
                        echo '<li><button type="button" class="addtoplaylist" action="../js/addtoplaylist.js">Add to playlist!</button> '.$info['artist'].' - '.$info['title'].' ('.$info['album'].') '.'<hr /></li>';
                    };
                    echo "</ul>";
                }elseif (isset($_REQUEST['kw']) && $_REQUEST['kw'] != ''){
                    //============================= Search for music ================================
                    $kws = $_REQUEST['kw'];
                    $kws = mysqli_real_escape_string($link, $kws);

                    $query = "select * from music where title like '%".$kws."%' or artist like '%".$kws."%'";
                    $result = mysqli_query($link, $query) or die (mysqli_error($link));
                    echo "<ul>";
                    while($info = mysqli_fetch_array( $result )){
                        echo '<li><button type="button" class="addtoplaylist" action="../js/addtoplaylist.js">Add to playlist!</button> '.$info['artist'].' - '.$info['title'].' ('.$info['album'].') '.'<hr /></li>';
                    };
                    echo "</ul>";
                }elseif(isset($_REQUEST['genre']) && $_REQUEST['genre'] != ''){
                    //=====================================Browse By Genre ===========================================
                    $genre = $_REQUEST['genre'];
                    $genre = mysqli_real_escape_string($link, $genre);
                    $gquery = "select music_id from musicgenre where genre_id = '$genre'";
                    $results = mysqli_query($link, $gquery) or die (mysqli_error($link));
                    $music=array();
                    while($id_result = mysqli_fetch_array($results)){
                        $music[] = $id_result['music_id'];
                    };
                    foreach($music as $song){
                        $query = "select * from music where music_id = '$song'";
                        $result = mysqli_query($link, $query) or die (mysqli_error());;
                        echo "<ul>";
                        while($info = mysqli_fetch_array($result)){
                            echo '<li><button type="button" class="addtoplaylist" action="../js/addtoplaylist.js">Add to playlist!</button> '.$info['artist'].' - '.$info['title'].' ('.$info['album'].') '.'<hr /></li>';
                        };
                        echo "</ul>";
                    };
                }else{
                // ================================ Default =========================================

                    $query = "select * from music";
                    $result = mysqli_query($link, $query) or die (mysqli_error());
                    echo "<ul>";
                    while($info = mysqli_fetch_array( $result )){
                        echo '<li><button type="button" class="addtoplaylist" action="../js/addtoplaylist.js">Add to playlist!</button> '.$info['artist'].' - '.$info['title'].' ('.$info['album'].') '.'<hr /></li>';
                    };
                    echo "</ul>";
            };

?>

这里是jquery文件,实际上是这样做的:

$(document).ready(function(){
    $(".addtoplaylist").click(function(){
        var $value = $(this);
        var $container = $value.parent();
        $container.appendTo("#playlist");
        $value.remove();

    })
})

所以这就是事情。当我第一次加载页面(如写网址并按回车)一切正常,所有的按钮和东西,但只要我点击一个类别或任何其他触发if / elseif / else语句的东西我的按钮停止工作。

有人可以帮我理解问题所在吗?我之前在我的分页结束时遇到了这个问题,结果删除了整个代码,因为我似乎无法找到答案。在分页代码中,当我点击触发切换的任何内容时,我的按钮消失了。我得到了应有的所有结果,所以这部分效果很好,但这个按钮让我发疯。我尝试了我能想到的一切。

2 个答案:

答案 0 :(得分:0)

你以错误的方式使用switch语句,使用if-elseif语句:

if (isset($_REQUEST['all']) && $_REQUEST['all'] != '') {
    //Stuff
} elseif (isset($_REQUEST['kw']) && $_REQUEST['kw'] != '') {
    //Stuff
} elseif ....

switch语句检查变量是否等于值:

$name = 'Alberto';
switch($name) {
  case 'Alberto':
     //We're here now
     break;
  case 'John':
     //Nothing to do here for the moment
     break;
  default:
     //Stuff for every values of $name that are different from 'Alberto' or 'John'
 }

因此您需要更改代码逻辑才能使用它

答案 1 :(得分:0)

解决了它。问题是我的结果是ajax生成的所以我只需要复制jquery部分并将其添加到成功函数中的ajax中。