根据选项运行sql查询,然后选择下拉菜单

时间:2019-07-20 04:19:14

标签: php html sqlsrv

我知道这是一个新手问题,但是我完全忘记了如何做。每当我更改下拉选择时,都应选择特定的SQL查询。例如,当我选择“贷款”时,其中所有带有“贷款”的accounttitle行应显示在表中,而当我选择“高级”时,则应更改表中的所有数据并仅显示带有“ 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>

1 个答案:

答案 0 :(得分:0)

所有php都在服务器上运行,因此在客户端(所选菜单项)上设置的值在php之后运行,并且不能在php中使用。重写代码以从存储在javascript中的php获取所有可能的值,或者使用从javascript到php的异步调用。