form没有更新动态html表的行数

nimxete2  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(232)

我有一个 Jmeter 板php页面,其中包含来自mysql数据库的动态记录表,分页为每个页面1000个。我添加了一个select下拉列表,其中有一些选项供用户更新显示的行数。select有javascript代码来自动提交表单onchange。
表单页:

<?php

 include('connect.php');

 // first check URL
 if (isset($_GET['amount'])) {
 $perpage = $_GET['amount'];
 }
 // Now check form
 if (isset($_POST['amount'])) {
 $perpage = $_POST['amount'];
 }
 // if nothing set, default:
 if(!isset($perpage)) {
 $perpage = 50;
 }

 if(isset($_GET['page']) && !empty($_GET['page'])){ // code to get the amount of records for HTML paginated pages
     $currentPage = $_GET['page'];
 } else {
     $currentPage = 1;
 }

 // Pagination code and query
 $startFrom = ($currentPage * $perpage) - $perpage;
 $totalEmpSQL = "SELECT * FROM `tblusers` WHERE Id NOT IN (1,2,3)";
 $allEmpResult = mysqli_query($conn, $totalEmpSQL);
 $totalEmployee = mysqli_num_rows($allEmpResult);
 $lastPage = ceil($totalEmployee/$perpage);
 $firstPage = 1;
 $nextPage = $currentPage + 1;
 $previousPage = $currentPage - 1;
 $empSQL = "SELECT Id, FirstName, LastName, Cell, Email, Address, Region
            FROM `tblusers` 
            WHERE Id NOT IN (1,2,3) 
            ORDER BY LastName 
            LIMIT $startFrom, $perpage";
 $empResult = mysqli_query($conn, $empSQL);

 ?>

 <!-- HTML Form -->
 <section class="panel">
 <header class="panel-heading">
             <div class="input-group">
<!-- JavaScript Filter Input -->
<input type="text" class="form-control"  id="search" onkeyup="myFunction()" 
placeholder="Soek lede..">
<div class="input-group-btn">
  <button class="btn btn-default" type="submit">
    <i class="glyphicon glyphicon-search"></i>
  </button>
</div>
</div>
</header>
    <div class="panel-body">

<div class="row datatables-header form-inline">

                            <div class="col-sm-4 col-md-4">

                            <!-- THE FORM THAT IS SUPPOSED TO UPDATE THE 
VARIABLE FOR AMOUNT QUERY -->
                            <form method="post" action="<?php echo 
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                <select id="amount" class="form-control" 
onchange="this.form.submit()">
                                         <option value="10">10</option>
                                         <option value="25">25</option>
                                         <option value="50">50</option>
                                         <option value="100">100</option>
                                         <option value="500">500</option>
                                    </select>
                           </form>
                            </div>
    <div class="col-sm-4 col-md-4">
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <?php if($currentPage != $firstPage) { ?>
                        <li class="page-item">
                            <a class="page-link" href="?page=<?php echo 
$firstPage ?>" tabindex="-1" aria-label="Vorige">
                                <span aria-hidden="true">Eerste</span>
                            </a>
                        </li>
                <?php } ?>
                <?php if($currentPage >= 2) { ?>
                        <li class="page-item"><a class="page-link" href="? 
 page=<?php echo $previousPage ?>"><?php echo $previousPage ?></a></li>
                <?php } ?>
                        <li class="page-item active"><a class="page-link" 
  href="?page=<?php echo $currentPage ?>"><?php echo $currentPage ?></a> 
 </li>
                            <?php if($currentPage != $lastPage) { ?>
                        <li class="page-item"><a class="page-link" href="?page=<?= $nextPage ?>&amount=<?= $perpage ?>"><?= $nextPage ?></a></li>
                        <li class="page-item">
                            <a class="page-link" href="?page=<?php echo 
  $lastPage ?>" aria-label="Volgende">
                                <span aria-hidden="true">Laaste</span>
                            </a>
                        </li>
                <?php } ?>
            </ul>
        </nav>
    </div>
    <div class="col-sm-4 col-md-4">
            <a class="btn btn-primary pull-right" style="margin:20px;" 
href="index.php?page=new">Laai nuwe lid</a>
    </div>
</div>
        <!-- THE DYNAMIC TABLE -->
        <div class="table-responsive">
            <table id="myTable" class="table table-hover table-striped 
    table-condensed mb-none">
                <?php
                    echo "<thead><th>Van:</th><th>Naam:</th><th>Selfoon: 
   </th><th>E-pos:</th><th>Adres:</th><th>Streek:</th><th>Aksie:</th> 
 </thead> 
   <tbody>";
                            while($emp = mysqli_fetch_assoc($empResult)){
                    ?>
                            <tr>
                                <?php echo "<tr><td>". $emp["LastName"] . " 
</td><td>". $emp["FirstName"] . "</td><td>". $emp["Cell"] . "</td><td>". 
 $emp["Email"] . "</td><td>". $emp["Address"] . "</td><td>". $emp["Region"] 
. 
"</td><td class='actions'><a href='index.php?page=edit&id=" . $emp["Id"] . 
"'><i class='fa fa-pencil'> Wysig | </i></a><a href='index.php? 
page=delete&id=" . $emp["Id"] . "'<i class='delete-row'><i class='fa fa- 
trash-o'> Verwyder | </i></a><a href='index.php?page=single&id=" . 
 $emp["Id"] 
 . "'<i class='delete-row'><i class='fa fa-comment-o'> SMS</i></a></td> 
</tr>";?>
                            </tr>
                        <?php } ?>
                    </tbody>
            </table>
        </div>
    </div>
</section>
<!-- dash panel end -->

<script>
function myFunction() {
  // Declare variables 
  var input, filter, table, tr, td, i;
  input = document.getElementById("search");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");

  // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
       if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
         tr[i].style.display = "";
       } else {
         tr[i].style.display = "none";
       }
    } 
  }
}
</script>

页面应该以默认的1000行/页的数量加载表,然后在select更改为100时更新,只加载100条记录。它目前没有显示任何记录,但当我删除选择它加载默认值。我的猜测是,我的代码处理的金额不正确?

yws3nbqq

yws3nbqq1#

尝试指定为默认值,如下所示:

$perpage = isset($_GET['amount']) ? $_GET['amount'] : 1000;

当前您根据 GET 值,然后在下一行中覆盖它。这样可以检查它是否在url中,如果不是,则使用默认值。
然后将该数量添加到分页链接中。例如,更改:

<li class="page-item"><a class="page-link" href="? 
 page=<?php echo $nextPage ?>"><?php echo $nextPage ?></a></li>

<li class="page-item"><a class="page-link" href="? 
 page=<?= $nextPage ?>&amount=<?= $perpage ?>"><?= $nextPage ?></a></li>

另外,你的表格是 POST ,但是select没有名称,因此没有发布值。另外,你要检查 $_GET . 所以我们需要先调整select:

<select id="amount" name="amount" class="form-control" 
onchange="this.form.submit()">

但是,这现在意味着您需要检查post以获取分页链接的select-yet-get。所以我们现在需要检查:

// first check URL
if (isset($_GET['amount'])) {
    $perpage = $_GET['amount'];
}
// Now check form
if (isset($_POST['amount'])) {
    $perpage = $_POST['amount'];
}
// if nothing set, default:
if(!isset($perpage)) {
    $perpage = 1000;
}
6uxekuva

6uxekuva2#

这个问题是由bootstrap附带的数据表插件引起的。我所需要做的就是将类datatable添加到表中,然后一切正常。

相关问题