通过SQL查询过滤并填充<select>

时间:2016-01-14 21:06:43

标签: php jquery sql

下面的代码运行并填充下拉列表。第2,第3和第4个查询也会填充下拉列表,而不是在此示例中。 我想在第一个下拉列表中选择所选内容并过滤其他3中的数据。 这是PHP: &LT; PHP require_once(&#39; conn.php&#39); 包括&#39; _menu.php&#39 ;; //获取进程 $ smt = $ stamp-&gt; prepare(&#39; SELECT tblProcess.PID,tblProcess.pName                     来自tblProcess                     WHERE tblProcess.pActive = 1                     ORDER BY tblProcess.pOrder&#39;); $ SMT-&GT;执行(); $ data = $ smt-&gt; fetchAll(); //获取类别 $ smt1 = $ stamp-&gt; prepare(&#39; SELECT tblCategory.CID,tblCategory.PID,tblCategory.cName                     来自tblCategory                     WHERE(((tblCategory.cActive)= 1))                     ORDER BY tblCategory.cOrder;&#39;); $ smt1-&GT;执行(); $ data1 = $ smt1-&gt; fetchAll(); //获取子类别 $ smt2 = $ stamp-&gt; prepare(&#39; SELECT tblCategorySub.CSID,tblCategorySub.CID,tblCategory.PID,tblCategorySub.csName                     FROM tblCategory INNER JOIN tblCategorySub ON tblCategory.CID = tblCategorySub.CID                     WHERE(((tblCategorySub.csActive)= 1))                     ORDER BY tblCategorySub.csOrder;&#39;); $ smt2-&GT;执行(); $ data2 = $ smt2-&gt; fetchAll(); //得到原因 $ smt3 = $ stamp-&gt; prepare(&#39; SELECT tblCause.CauseID,tblCause.caName                     从tblCause                     WHERE(((tblCause.caActive)= 1))                     ORDER BY tblCause.caSortOrder&#39;); $ smt3-&GT;执行(); $ data3 = $ smt3-&gt; fetchAll(); ?&GT; 这是填充一个下拉列表的HTML。我只包括一个来缩短这个问题。 &LT; HTML&GT; &LT; HEAD&GT;     &lt; script type =&#34; text / javascript&#34; SRC =&#34; HTTP://code.jquery.com/jquery-latest.min.js"&GT;&LT; /脚本&GT;     &lt; link type =&#34; text / css&#34;的rel =&#34;样式表&#34; HREF =&#34; stylesheet.css中&#34; /&GT;      &lt; script src =&#34; ../ script / _DDDisable.js&#34;&gt;&lt; / script&gt;     &LT;标题&GT;&LT; /标题&GT; &LT; /头&GT; &LT;身体GT;     &lt; div id =&#34; header&#34;&gt;         &lt; p id =&#34; name&#34;&gt; STAMPING TROUBLE REPORT&lt; / p&gt;         &lt; a href =&#34; mailto:you@yourdomain.com"&gt;&lt; p id =&#34; email&#34;&gt;&lt; / p&gt;&lt; / a&gt;     &LT; / DIV&GT;     &lt; div class =&#34; right&#34;&gt;         &lt; h5&gt;流程&amp; nbsp&amp; nbsp&lt; select class =&#34; form-control&#34;命名=&#34;处理&#34; ID =&#34;处理&#34;&GT;             &lt; option value =&#34;选择&#34;&gt; - 选择处理 - &lt; /选项&gt;             &lt;?php foreach($ data as $ row):?&gt;                 &lt; option value = $ row [PID]&gt;&lt;?= $ row [&#34; pName&#34;]?&gt;&lt; / option&gt;             &lt;?php endforeach?&gt;&lt; / select&gt;&lt; / h5&gt;     &LT; / DIV&GT; &LT; /体&GT;

1 个答案:

答案 0 :(得分:3)

像这样创建HTML标记,data-belongsto属性告诉我们应该在DOM中填充哪个下拉列表。 ID我们将在switch语句中向服务器发送用例。

<select class="form-control" name="Process" id="Process" data-belongsto="Category">


$('select.form-control').on('change', function(e){
    e.preventDefault();

    //cacheable reference to our select
    var $this = $(this);
    $.ajax({
        url: '/path/to/my/file.php',
        type: 'POST',
        data: {
            'request_type'  : $this.prop('id'),
            'request_value' : $this.val()
        },
        dataType: 'json',
        success: function(response){
            //loop over our response and append to the `belongsto` we defined earlier
            $.each(response.option, function(i,opt){
                $('#'+$this.data('belongsto').append('<option value="'+opt.PID+'"> '+opt.pName+'</option>');
            });
        }
    });
});

然后在您的文件中,只需使用默认值切换语句。我们假设process将始终首先运行。

if(!isset($_POST['request_type']){
    //Get Processes
    $smt = $stamp->prepare('SELECT tblProcess.PID, tblProcess.pName
                    FROM tblProcess
                    WHERE tblProcess.pActive=1
                    ORDER BY tblProcess.pOrder');
    $smt->execute();
    $data = $smt->fetchAll();

} elseif(isset($_POST['request_type']) && isset($_POST['request_value'])) {
    switch($_POST['request_type']):
        case 'Categories':

           //Get Categories
           $smt1 = $stamp->prepare('SELECT tblCategory.CID, tblCategory.PID, tblCategory.cName
                    FROM tblCategory
                    WHERE (((tblCategory.cActive)=1))
                    ORDER BY tblCategory.cOrder;');
           $smt1->execute();
           $data = $smt1->fetchAll();
           break;

        case 'SubCategories':
           //Get SubCategories
           $smt2 = $stamp->prepare('SELECT tblCategorySub.CSID, tblCategorySub.CID, tblCategory.PID, tblCategorySub.csName
                    FROM tblCategory INNER JOIN tblCategorySub ON tblCategory.CID = tblCategorySub.CID
                    WHERE (((tblCategorySub.csActive)=1))
                    ORDER BY tblCategorySub.csOrder;');
           $smt2->execute();
           $data = $smt2->fetchAll();

        case 'Cause':

            //Get Cause
            $smt3 = $stamp->prepare('SELECT tblCause.CauseID, tblCause.caName
                    FROM tblCause
                    WHERE (((tblCause.caActive)=1))
                    ORDER BY tblCause.caSortOrder');
            $smt3->execute();
            $data = $smt3->fetchAll();
            break;
    endswitch;

    //exit to make sure nothing else is sent back that would alter our json response types validity
    echo json_encode($data, true); exit();
}

现在只需修改您的SQL语句以检查各自的$_POST['request_value'],您就会很好。