我有两张桌子 - 公司和客户。它们每个都有一个标记为company_id
的相同行,因此我可以为每个客户分配一个公司。
首先创建一家公司。
然后创建客户端,公司在此过程中与之关联。
然后创建订单......
对于订单,我有一个名为orders的表。将数据插入表格时,我有一个带有两个下拉框的表单。第一个下拉列表是选择订单所针对的公司。使用以下内容填充:
$getCompany = mysqli_query($db, "SELECT * FROM companies ORDER BY company_name ASC");
<label for="company_id">Company</label>
<select class="form-control" name="company_id" id="company_id">
<option disabled selected></option>
<?php
// If there are results, output each row.
if($getCompany) {
while($company = mysqli_fetch_assoc($getCompany)) { ?>
<option value="<?php echo $company['company_id']; ?>">
<?php echo $company['company_name']; ?>
</option>
<?php }
}
?>
</select>
第二个下拉列表是相同的,而是调用客户端&#39;名。
$getClient = mysqli_query($db, "SELECT * FROM clients ORDER BY client_fname ASC");
<select class="form-control" name="client_id" id="client_id">
<option disabled selected></option>
<?php
// If there are results, output each row.
if($getClient) {
while($client = mysqli_fetch_assoc($getClient)) { ?>
<option value="<?php echo $client['client_id']; ?>">
<?php echo $client['client_fname']." ".$client['client_lname']; ?>
</option>
<?php }
}
?>
</select>
如果我从第一个下拉列表中选择CompanyA,如何在第二个下拉列表中仅显示与该选择相关联的客户?
答案 0 :(得分:0)
像这样使用ajax
的Javascript
$('.company_id').change(function()
{
var company_id = $('.company_id').val();
var request = $.ajax({
url: "your_ajax.php",
method: "POST",
data: {
company_id: company_id,
action: 'get_clients_by_company_id'
}
});
request.done(function (msg) {
$('#client_id').val(msg);
});
});
你的AJAX中的PHP代码
<?php
if (isset($_POST['action']) and $_POST['action'] == 'get_clients_by_company_id') {
$company_id = $_POST['company_id'];
if (!empty($company_id)) {
$getClient = mysqli_query($db, "SELECT * FROM clients WHERE company_id = '$company_id' ORDER BY client_fname ASC");
while ($client = mysqli_fetch_assoc($getClient)) {
?>
<option value="<?=$client['client_id']; ?>">
<?=$client['client_fname'] . " " . $client['client_lname']; ?>
</option>
<?php
}
}
}
?>