答案 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']
,您就会很好。