Advertisement
Syehaji93

Search Data By Date Range

Sep 8th, 2022
1,360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | Software | 0 0
  1. // form view
  2. <form autocomplete="off">
  3. <?= csrf_field(); ?>
  4.     <div class="card-body">
  5.         <div class="row">
  6.             <div class="col-md-2">
  7.                 <div class="form-group">
  8.                     <label for="fromDate">From Date:</label>
  9.                     <div class="input-group date">
  10.                         <input type="hidden" class="txt_csrfname" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>" />
  11.                         <input type="text" class="form-control datepicker" id="fromDate" name="fromDate">
  12.                         <div class="input-group-addon">
  13.                             <span class="glyphicon glyphicon-th"></span>
  14.                         </div>
  15.                     </div>
  16.                 </div>
  17.             </div>
  18.             <div class="col-md-2">
  19.                 <div class="form-group">
  20.                     <label for="toDate">To Date:</label>
  21.                     <div class="input-group date">
  22.                         <input type="text" class="form-control datepicker" id="toDate" name="toDate">
  23.                         <div class="input-group-addon">
  24.                             <span class="glyphicon glyphicon-th"></span>
  25.                         </div>
  26.                     </div>
  27.                 </div>
  28.             </div>
  29.             <div class="col-md-3">
  30.                 <div class="form-group">
  31.                     <label for=""><font color="white"> _</font></label>
  32.                     <div class="input-group">
  33.                         <button type="submit" id="submit" name="submit" value="search" class="btn btn-primary col-sm-3">Search</button>
  34.                     </div>
  35.                 </div>
  36.             </div>
  37.         </div>
  38.     </div>
  39. </form>
  40.  
  41.  
  42. //controller
  43. public function searchData()
  44. {
  45.     $fromDate  = $this->request->getVar('fromDate');
  46.     $toDate    = $this->request->getVar('toDate');
  47.    
  48.     $query = $this->db->table('tb_cek_harian')->getWhere(['ch_delivery >=' => $fromDate, 'ch_delivery <=' => $toDate]);
  49.     $data = $query->getResult();
  50.  
  51.     var_dump($data);
  52.     echo json_encode($data);
  53.  
  54. }
  55.  
  56. // ajax
  57. //button search
  58. $('#submit').on('click', function(){
  59.     event.preventDefault();
  60.     var table = $('checksheet');
  61.        
  62.     // var fromDate = document.getElementById("fromDate").value;
  63.     // var toDate = document.getElementById("toDate").value;
  64.  
  65.     var fromDate = $("#fromDate").val();
  66.     var toDate = $("#toDate").val();
  67.     var x = 1;
  68.        
  69.     if(fromDate == ''){
  70.     error_firstname = 'please enter start date';
  71.     alert(error_firstname);
  72.     }
  73.     else if(toDate == ''){
  74.     error_lastname = 'please enter end date';
  75.     alert(error_lastname);
  76.     }
  77.     else{
  78.         console.log(fromDate);
  79.         console.log(toDate);
  80.  
  81.     $.ajax({
  82.         url: "<?php echo base_url('/checksheet/search'); ?>",
  83.         type: 'POST',
  84.         dataType: 'JSON',
  85.         data: {
  86.             fromDate:fromDate, toDate:toDate
  87.         },
  88.         success: function(data) {
  89.         console.log(data);
  90.        
  91.         var html = '';
  92.         var i;
  93.         for(i=0; i<data.length; i++){
  94.            html += '<tr>'+
  95.                       '<td style="text-align:center;">'+data[i].ch_name+'</td>'+
  96.                       '<td style="text-align:center;">'+data[i].ch_lot+'</td>'+
  97.                       '<td style="text-align:center;">'+data[i].ch_delivery+'</td>'+
  98.                       '<td style="text-align:center;">'+data[i].ch_result+'</td>'+
  99.                       '</tr>';
  100.          }
  101.          $('#listRecord').html(html);
  102.  
  103.         },
  104.         error: function(xhr, textStatus, error) {
  105.             console.log(xhr.statusText);
  106.             console.log(textStatus);
  107.             console.log(error);
  108.         }
  109.     });*/
  110.     }
  111.        
  112. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement