为什么我的PHP代码不使用复选框信息来遍历我的交换机?

时间:2015-07-21 13:41:23

标签: javascript php jquery html

我有一个网站,使用php在页面上呈现模块。目前唯一辨别该网站所显示的模块是您是否登录。如果您已登录,则会全部显示,如果没有,则只显示少数几个。我试图做的是允许用户使用复选框过滤出模块,但我的代码不起作用。我可能会遗漏一些简单的东西,所以希望一组新的眼睛会发现我所缺少的东西。这是我到目前为止所做的。

 <form >
      <input type="checkbox" name="Categories" value="Categories"> example1<br>
      <input type="checkbox" name="Categories1" value="Categories1">   example2<br>
      <input type="submit" value="Submit">
 </form>

<?php
    if (isset($_POST['Categories']))
    {
        $variable = 1;
    }
    elseif (isset($_POST['Categories1']))
    {
        $variable = 2;
    }
    else
    {
        echo "You haven't picked anything!";
    }
?>
<?php switch($variable): 
    case 1: ?>
        <div class="col-md-4 module" id="exampleID"> example1 </div>
        <div class="col-md-4 module" id="exampleID"> example2 </div>
    <?php break;?>
    <?php case 2: ?>
        <div class="col-md-4 module" id="exampleID"> example3 </div>
        <div class="col-md-4 module" id="exampleID"> example4 </div>
    <?php break;?>
    <?php default;?>
        <div class="col-md-4 module" id="exampleID"> example1 </div>
        <div class="col-md-4 module" id="exampleID"> example2 </div>
        <div class="col-md-4 module" id="exampleID"> example3 </div>
        <div class="col-md-4 module" id="exampleID"> example4 </div>
<?php endswitch;?>

我得到的问题是它始终显示一切。不仅仅是它应该的4个,而且4个加上其他情况下的所有内容。我正在寻找的愿望是,如果我选中第一个框,那么页面会显示:

example1
example2

如果我选中了第二个方框:

example3
example4

如果我没有检查任何方框:

example1
example2
example3
example4

希望你们能帮助我。

编辑 -

这是我目前得到的输出:

Example1
Example2
Example3
Example4
Example1
Example2
Example3
Example4

2 个答案:

答案 0 :(得分:0)

<form method="post"> 

这应该会有所帮助如果你没有为方法命名,而不是发布。

为了更好地阅读/清理代码,您应该在switch语句中使用echo。

答案 1 :(得分:0)

已经回答了:

以下是我最终提出的做我想要的代码。我还在添加更新的复选框。希望这可以帮助别人。

JQuery的:

<script type="text/javascript">
$(document).ready( function() {
                <!-- Sets All The Modules To Show By Default -->
                startup();

                $(".CategorySelector").click( function() {
                                //$(this).value

                                //Hide All Modules
                                $('div.module').each( function() {
                                            $(this).hide();                                     
                                })
                                var chooseCat = 0;
                                //Loop through category selectors and show modules
                                $("input.CategorySelector").each( function() {
                                                if ($(this).is(':checked')) {
                                                    chooseCat++;
                                                    showCategory($(this).val())
                                                } 

                                })      

                                if (chooseCat == 0){
                                    startup();
                                }
                })
});


function startup() {
                $('div.module').each( function() {
                                $(this).hide();                                     
                })                  
                $('div.module').each( function() {
                                $(this).show();
                })
}

function showCategory(Category) {
                var selector = '[categories~="'+Category+'"]';
                $(selector).each( function() {
                                $(this).show();
                })
}
</script> 

复选框:

<input type="checkbox" class="CategorySelector" value="value1">
Example1<br>
<input type="checkbox" class="CategorySelector" value="value2">
Example2<br>
<input type="checkbox" class="CategorySelector" value="value3">
Example3<br>
<input type="checkbox" class="CategorySelector" value="value4">
Example4<br>

我想要尝试做的一件事是将复选框嵌入到iframe中,然后像滑动标签一样创建,这样导航就不会打扰。但是,首先,我不知道如何从iframe调用数据。其次,我不知道如何做滑动动画。值得期待的事情:)