【发布时间】:2019-11-28 23:57:40
【问题描述】:
我知道这是一个新手问题,但我完全忘记了如何做到这一点。每当我更改下拉选择时,都应选择特定的 SQL 查询。例如,当我选择“LOAN”时,所有带有“LOAN”的accounttitle 行都应该显示在表格中,当我选择“ADVANCE”时,它应该更改表格中的所有数据并只显示数据里面有“ADVANCE”。
我尝试通过刷新来设置它,不幸的是它不会切换到第二个选项。
这是我的选择
<?php
$sql = "SELECT accountcode, accounttitle, accounttype FROM earningsamendmentaccount";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
?>
<label for="select_account_title" class="col-sm-3 control-label">Select Account Title</label>
<div class="col-sm-9">
<select class="form-control" id="select_account_title" name="select_account_title" style="text-transform:uppercase" required>
<?php
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
$_POST['accountcode']= $row['accountcode'];
$_POST['accounttitle']= $row['accounttitle'];
echo "<option value=".$_POST['accountcode'].">".$_POST['accounttitle']."</option>";
}
?>
</select>
</div>
这是我的查询
$sql = "SELECT referenceno, employeeidno, accounttitle, 'ON PROGRESS' as debit, postedby, approvedby, notedby, credit FROM earningsamendment where accounttitle= '" . $_POST['accounttitle'] . "'";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
它运行没有错误,不幸的是它只选择了下拉列表的最后一部分,当我更改下拉列表值时没有发生任何变化。
编辑:这是<form> 和<table> 所在的位置。
<div class="col-xs-12">
<div class="box">
<form class="form-inline">
<div class="box-header with-border">
<a href="#addnew" data-toggle="modal" class="btn btn-primary btn-sm btn-flat"><i class="fa fa-plus"></i> New</a>
<div class="form-group">
<?php
$sql = "SELECT accountcode, accounttitle, accounttype FROM earningsamendmentaccount";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
?>
<label for="select_account_title" class="col-sm-3 control-label">Select Account Title</label>
<div class="col-sm-9">
<select class="form-control" id="select_account_title" name="select_account_title" style="text-transform:uppercase" onchange="this.form.submit()" required>
<?php
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
$value = $row['accountcode'];
$value2 =$row['accounttitle'];
$_POST['accountcode']= $value;
$_POST['accounttitle']= $value2;
echo "<option value=".$_POST['accountcode'].">".$_POST['accounttitle']."</option>";
}
?>
</select>
</div>
</div>
</form>
<div class="box-body">
<table id="example1" class="table table-bordered">
<thead>
<th>Reference No.</th>
<th>Employee ID</th>
<th>Account Title</th>
<th>Amount</th>
<th>Activity</th>
<th>Posted By</th>
<th>Validated By</th>
<th>Noted By</th>
<th>Tools</th>
</thead>
<tbody>
<?php
$sql = "SELECT referenceno, employeeidno, accounttitle, 'ON PROGRESS' as debit, postedby, approvedby, notedby, credit FROM earningsamendment where accounttitle= '" . $_POST['accounttitle'] . "'";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
echo "
<tr>
<td>".$row['referenceno']."</td>
<td>".$row['employeeidno']."</td>
<td>".$row['accounttitle']."</td>
<td>".$row['credit']."</td>
<td>".$row['debit']."</td>
<td>".$row['postedby']."</td>
<td>".$row['approvedby']."</td>
<td>".$row['notedby']."</td>
<td>
<button class='btn btn-success btn-sm edit btn-flat' data-id='".$row['referenceno']."'><i class='fa fa-edit'></i> Edit</button>
<button class='btn btn-danger btn-sm delete btn-flat' data-id='".$row['referenceno']."'><i class='fa fa-trash'></i> Delete</button>
" ?>
<?php if (empty($row['approvedby'])) { echo " <button class='btn btn-warning btn-sm approve btn-flat' data-id='".$row['referenceno']."'><i class='fa fa-check-square-o'></i> Approve</button> "; } ?>
<?php "</td>
</tr>
";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
</div>
【问题讨论】:
-
我不确定你的意思是什么:“我试图通过刷新来设置它,不幸的是它不会切换到第二个选项。”此外,我没有看到任何可以刷新数据的代码。是否可以帮助澄清您编码的内容以及具体出了什么问题?一个想法是在
<select>更改时提交表单,可能是异步的。 -
我想您是在问如何在下拉列表更改时执行查询并输出一些数据。那正确吗?请注意,手动设置
$_POST值will not repost the data to the page。您可能想要包含<form>元素和 submit the form when the<select>option changes。 -
@showdev 是的,正确,我实际上正在尝试
onchange="this.form.submit()",但它不会改变输出。 -
@showdev 只是为了澄清您提供的指南,我应该用
<form>附上表格,对吗? -
我不确定您的
<table>的位置,但至少将<select>包含在<form>中。这样select_account_title值可以通过PHP 的$_POST数组提交和检索。至少这是处理它的一种方法。如果它不起作用,您可能会考虑编辑您的帖子,以包含您到目前为止所获得的内容以及似乎出了什么问题。