dstnlhhv791576 2016-11-22 16:25
浏览 62

在下拉列表中显示特定内容

I have a little question about some code I found online, the main issue is that I want to to have 2 dropdowns, the first dropdown looks like this:

     <select name="member_group">
        <option value="0">Vælg en gruppe først</option>
        <?php
                $sql = "SELECT * FROM member_groups WHERE company = '$row_cp[id]' ORDER BY id DESC";
                $result = $conn->query($sql);

                if (mysqli_num_rows($result) > 0) {
                    while($row = mysqli_fetch_assoc($result)) {
                    print '<option rel="' . $row[id] . '" value="' . $row[id] .'">- ' . $row[name] . '</option>';
                    }
                }
         ?>
      </select> 

In this section, I want the different groups I've created, to appear directly from the database.

Let's say we have 2 groups.

ID: 1 - Basic ID: 2 - Advanced

If I select ID: 2 - Advanced, I want in the next dropdown, only to show prices that are connected to ID: 2, all that happens in the database also, therefore my 2'nd dropdown would look like this.

      <select name="products" class="cascade">
        <?php
              $sql = "SELECT * FROM products WHERE company = '$row_cp[id]' AND available_for = '$_POST[member_group]' ORDER BY id DESC";
              $result = $conn->query($sql);

              if (mysqli_num_rows($result) > 0) {
                while($row = mysqli_fetch_assoc($result)) {
              print '<option class="' . $_POST[member_group] . '" value="' . $visgg[id] .'">- ' . $visgg[name] . '</option>';
                   }
               }
       ?>
     </select>  

My issue here is, that in order only to show the ones that are linked to ID: 2, I must create this in SQL query

available_for = '$_POST[member_group]'

But since everything is happening in the same form, I don't get a value from $_POST[member_group].

I've used this code, I found on a forum

<script>
$(document).ready(function(){
    var $cat = $('select[name=member_group]'),
        $items = $('select[name=products]');

    $cat.change(function(){
        var $this = $(this).find(':selected'),
            rel = $this.attr('rel'),
            $set = $items.find('option.' + rel);

        if ($set.size() < 0) {
            $items.hide();
            return;
        }

        $items.show().find('option').hide();

        $set.show().first().prop('selected', true);
    });
});
</script> 

<style type="text/css">
.cascade {
    display: none;
}

</style>    

The demo code I downloaded, worked fine on jsfiddle, so I guess my question is how can I get the $_POST[member_group] to the text dropdown, perhaps with some jQuery?

I always get great help here, so I hope you can help me out :o)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害