如何在单选按钮上执行不同的查询?

时间:2018-10-12 06:27:41

标签: javascript php html mysql ajax

我试图在单选按钮的不同选择上获取不同的结果。我的代码中有三个按钮。我想当用户选择所有字段(如select tsm,select sr,from,to)和任何单选时执行不同的查询。我想要当用户选择第一台收音机然后执行一个查询时,如果用户选择第二台收音机然后执行不同的查询,依此类推。但是,当我尝试执行此操作时,我的结果没有到来,这意味着我的数据没有传递给查询,因此它无法获取结果。

<select class="form-control" name="tsm_id" id="tsm_id">
	<option value=""><b>SELECT TSM</b></option>
	<?php echo fill_year($connect); ?> 
</select>
<br><br>

<select class="form-control" name="sr_id" id="sr_id">
  <option value="" selected>SELECT SR</option>   
</select>
<br><br><br><br>
<div class="row">
     <div class="input-daterange">
      <div class="col-md-4">
       <label>From:</label><input type="text" name="start_date" id="start_date" data-provide="datepicker" class="form-control" />
     </div>
     <div class="col-md-4">
       <label>To:</label><input type="text" name="end_date" id="end_date" data-provide="datepicker" class="form-control" />
     </div>      
   </div>
   <div><br><br><br><br>
<label>Visited during this period:</label><input type="radio" name="options" value="first" id="first" class="options">
<label>Not Visited during this period:</label><input type="radio" name="options" value="second" id="second" class="options">
<label>Never Visited:</label><input type="radio" name="options" value="third" id="third" class="options"><br>
    <input type="button" name="search" id="search" value="Search" class="btn btn-info" />
  </div>
</div><br><br><br>

<script type="text/javascript">
  $(document).ready(function(){  
   $('#sr_id').change(function(){  
    $('.options').click(function(){
     var sr_id = $('#sr_id').val();  
     var start_date = $('#start_date').val();
     var end_date = $('#end_date').val();
    var first=$(this).val();
     $.ajax({  
      url:"http://localhost/sales/fetch_retailer.php",  
      method:"GET",
      data:{sr_id:sr_id,start_date:start_date,end_date:end_date},  
      dataType:"json",                  
      success:function(data){ 
        alert("success")
        var test=JSON.stringify(data);
        console.log(test)
        $('#count_data').html('');
        for (var i in data)
        {
          $("#count_data").append("<tr></td><td class='text-center'>"+data[i].retailer_shop_name+"</td><td class='text-center'>"+data[i].last_visit+"</td></tr>");
        }
      }
    });
   }); 
  });
 });
</script>

if(isset($_GET['options']) && !empty($_GET['options'])){
    if($_GET['options'] == 'first'){
   $sql=sprintf("SELECT 
   tbl_retailer.retailer_shop_name,
   tbl_retailer.retailer_id,
   max(tbl_visit_master.visit_DateTime) as last_visit
   from tbl_retailer
   left join tbl_member on tbl_member.employee_id=tbl_retailer.sr_id
   left join tbl_visit_master on tbl_retailer.retailer_id=tbl_visit_master.retailer_id where tbl_retailer.sr_id='".$_GET["sr_id"]."' and tbl_retailer.isgroup=1 
   group by retailer_id having (max(tbl_visit_master.visit_DateTime)) BETWEEN '".$_GET["start_date"]."' AND '".$_GET["end_date"]."'");
  }  

2 个答案:

答案 0 :(得分:0)

您正在尝试在像这样的$_GET['options']这样的php代码中获取选项的数据,但没有将其传递给ajax的data属性

data:{sr_id:sr_id,start_date:start_date,end_date:end_date}

尝试一下

data:{sr_id:sr_id, start_date:start_date, end_date:end_date, options:first}

答案 1 :(得分:0)

用以下代码替换ajax调用,然后尝试。

            $.get( 
            url:"http://localhost/sales/fetch_retailer.php", 
            data:{
                    sr_id: sr_id, 
                    start_date: start_date, 
                    end_date: end_date
                }
            )
            .done(function( data ) {
            alert( "Data Loaded: " + data );
            });