首页 > 解决方案 > 当我在 Bootstrap 日期范围选择器中更改日期时,更改的日期不会触发

问题描述

在下面的代码中,我想更新在cashadvance查询中使用的fromto变量中输入的选定日期(通过 Bootstrap 日期范围选择器)。我认为它们是日期范围选择器回调函数的问题。

两个变量range_torange_from的值在 id=reservation 的输入标签中获取,然后在 from 和 to using 中获取它们的值,isset($_GET[range])但它不起作用。

代码:payrol.php

<?php
    include('db.php');
        $range_to = date('m/d/Y');
        $range_from = date('m/d/Y', strtotime('-30 day', strtotime($range_to)));
    ?>
    
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">    
    </head>
    <body>
    
    <div class="container-fluid">
    
    
     <div class="box-header with-border">
            <div class="pull-left"> 
                <input type="text" class="form-control pull-right col-sm-13" id="reservation" name="date_range" value="<?php echo (isset($_GET['range'])) ? $_GET['range'] : $range_from.' - '.$range_to; ?>">&nbsp;
            </div> 
      </div>
    
    
            <table>
            <thead>
                <tr>
                      <th>Employee ID</th>
                      <th>Gross</th>
                      <th>Cash Advance</th>
                      <th>Range</th>
                </tr>
            </thead>
        
        <?php                    
                        $to = date('Y-m-d');
                        $from = date('Y-m-d', strtotime('-30 day', strtotime($to)));
    
                        if(isset($_GET['range'])){
                          $range = $_GET['range'];
                          $ex = explode(' - ', $range);
                          $from = date('Y-m-d', strtotime($ex[0]));
                          $to = date('Y-m-d', strtotime($ex[1]));
                        }
                      /* **********Fetch Record From Employee ************ */
                      $sql = "SELECT * from employee";
    
                      $query = $con->query($sql);
                      $total = 0;
                      while($row = $query->fetch_assoc()){
                      $empid = $row['id'];
                      /* ********* Cash Advance Query********* */
                          $casql = "SELECT *, SUM(aamount) AS cashamount FROM advance WHERE id='$empid' AND adate BETWEEN '$from' AND '$to'";
                          
                          $caquery = $con->query($casql);
                          $carow = $caquery->fetch_assoc();
                          $cashadvance = $carow['cashamount'];
    
                          $gross = $row['Salary'] ;
    
                          
                          echo "
                            <tr>
                              <td>".$row['empid']."</td>
                              <td>".number_format($gross, 2)."</td>
                              <td>".number_format($cashadvance, 2)."</td>
                              <td>".$from."-".$to. "</td>
                            </tr>
                          ";
                      }
                      ?>
            </table>
        </div>
    
    
    <script>
    $(function(){
      $("#reservation").on('change', function(){
        var range = encodeURI($(this).val());
        window.location = 'payrol.php?range='+range;
      });
    });
    </script>
    
    <!--************Date Range Picker********************-->
    
    <script> 
    $(function() {
      //Date range picker
      $('#reservation').daterangepicker()
    
      $('input[name="date_range"]').daterangepicker({
        opens: 'left'
      }, function(start, end, label) {
        console.log("A new date selection was made: " + start.format('YYYY-MM-DD') + ' - ' + end.format('YYYY-MM-DD'));
      });
    });
    </script>
    
    

</body>
</html>

标签: javascriptphpdaterangepicker

解决方案


如果对象绑定了事件,请在浏览器中检查。如果没有,您可能需要等待文档准备好才能装订:

$(document).ready(function(){
     $("#reservation").on('change', function(){
        var range = encodeURI($(this).val());
        window.location = 'payrol.php?range='+range;
     });
 });

推荐阅读