AJAX函数调用提交按钮在PHP CodeIgniter

时间:2018-02-28 07:13:07

标签: javascript php jquery ajax

我正在研究CI项目,因为我正在datatables中加载数据库中的数据并在该数据表上应用filters。当我从drop-down listbox中选择一个值并点击过滤按钮时,它会过滤数据,但下拉列表框中的值会重置。为此,我尝试使用AJAX function加载数据,但我的AJAX call无法处理按钮点击事件。

以下是我的代码:

控制器:

public function index()
{
    $user = $this->ion_auth->user()->row();
    $data['username'] = $user->username;

    $data['user_id'] = $user->id;
    $user_id = $user->id;
    $data['groupId'] = $this->l->groupId($user_id);
    $data['group'] = $data['groupId']['0']->group_id;
    //$propId = $this->input->post('property_id');
    $propertyType=$this->input->post('property_type');
    $area=$this->input->post('area_id');
    $stageId=$this->input->post('property_status');

    $clustersID['cluster']=$this->input->post('cluster_id');

    if(!empty($clustersID['cluster'])){
      // Array contains values, everything ok
      $clusterString = implode(',', $clustersID['cluster']);
    } else {
      // Array is empty
      $clusterString = 0;
    }

    if ($propertyType =='') {
        $propertyType=0;
    }
    if( $area ==''){
        $area =0;
    }
    if($clusterString == ''){
        $clusterString=0;
    }
    if ($stageId == '') {
        if($data['group']==1)
        {
            $stageId=0;
        } else {
            $stageId=4;
        }
    }
    $propDetail = $this->input->post('propDetail');

    $data['areas'] = $this->p->area();
    $data['clusters'] = $this->p->cluster();
    $data['property_stage'] = $this->p->selectPropertyStage();
    //$data['clusters'] = $this->p->clusterAll($area);
    $data['title'] = 'Property List';
    $data['property_type'] = $this->p->selectPropertyType();
    $data['properties'] = $this->p->getPropertyByAreaCluster($propertyType, $area, $clusterString, $stageId);

    $data['property'] = json_encode($data['properties']);

    $data['prop'] = json_decode($data['property']);

    //$data['props'] = $this->p->PropView($propId);
    $this->load->view('template/header', $data);
    $this->load->view('Property/property_view', $data);
    $this->load->view('template/footer');
}

型号:

public function getPropertyByAreaCluster($propertyType, $area, $clusterString, $stageId)
{
    $query = $this->db->query("call fetch_propertyType_Area_Cluster_Stage($propertyType,$area,'$clusterString',$stageId)");
    if ($query) {
        $data = $query->result();
        $query->next_result(); 
        $query->free_result();
        return $data;
    }else{
        return false;
    }
}

查看:

    <div class="row" id="refresh">
    <div class="col-md-12">
        <div class="panel panel-default">
        <div class="panel-body">
            <div style="padding-left: 10px;">
  <form class="form-inline" action="" method="POST">
        <div class="form-group">
        <label>Area:</label>
            <select class="form-control select2 country" name="area_id" id="areaId">
            <option value="0">All</option>
            <?php foreach ($areas as $area) { ?>
            <option value="<?php echo $area->area_id; ?>"><?php echo $area->area_name; ?></option>
            <?php } ?>                     
            </select>
        </div>
        <div class="form-group">&nbsp;&nbsp;</div>
        <div class="form-group">
        <label>Cluster:</label>
            <select class="form-control select2" name="cluster_id[]" id="cluster_id" multiple="multiple">
            <option value="0">All</option>
            <?php foreach ($clusters as $cluster){ ?>
            <option value="<?php echo $cluster->cluster_id; ?>"><?php echo $cluster->cluster_name; ?></option>
            <?php } ?>
            </select>
        </div>
        <div class="form-group">&nbsp;&nbsp;</div>
        <div class="form-group">
            <label>Property Type:</label>
              <select class="form-control select2" name="property_type" id="property_type">
                <option value="0">All</option>
                <?php if ($property_type) { foreach ($property_type as $type) {?>
                <option value="<?= $type->property_type_id; ?>"><?= $type->property_type_name;?></option>
                <?php }} ?>
              </select> 
          </div> 
          <div class="form-group">&nbsp;&nbsp;</div>
          <div class="form-group">
            <label class="control-label">Stage:</label>
               <select class="form-control select2" name="property_status" id="property_status">
               <option value="0">All</option>   
               <?php foreach ($property_stage as $stage) { ?>  
               <option value="<?= $stage->stage_id; ?>"><?= $stage->stage_name; ?></option>
               <?php } ?>
              </select>
        </div> 
            <div class="form-group">&nbsp;&nbsp;</div>          
            <button type="submit" onclick="showmore()" class="btn btn-primary">Filter</button>
            <div class="form-group">&nbsp;&nbsp;</div>

  </form>
        <div>&nbsp;</div>
        <div class="content">

<table id="example1" class="table table-striped table-bordered" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Code</th>
      <th>Date</th>
      <th>Type</th>
      <th>ASYS</th>
      <th>Address1</th>
      <th>City</th>
      <th>Status</th>
      <th>Landlord</th>
      <th>Rooms</th>
      <th>Edit</th>
      <th>Action</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <th>Code</th>
      <th>Date</th>
      <th>Type</th>
      <th>ASYS No</th>
      <th>Address1</th>
      <th>City</th>
      <th>Status</th>
      <th>Landlord</th>
      <th>Rooms</th>
      <th>Edit</th>
      <th>Action</th>
    </tr>
  </tfoot>
  <tbody>
    <?php if( $prop ) { foreach ($prop as $property) { ?>
    <tr>
      <td>
        <i>
          <a href="<?= base_url('Property/propModel/'.$property->property_id);?>" data-toggle="modal" data-target="#myModal">
            <?= $property->property_code; ?>
          </a>
        </i>
      </td>
      <td><?= $property->property_added_date; ?></td>
      <td><?= $property->property_type_name; ?></td>
      <td><?= $property->property_ASYS_no; ?></td>
      <td>
        <a href="<?= base_url('Property/propertyDetails/'.$property->property_id);?>">
          <?= $property->property_address_1; ?>    
        </a>
      </td>
      <td><?= $property->cluster_name; ?></td>
      <td>
        <?php if($property->property_risk_status==0) { ?>
          <input type="text" name="Color" id="Color" style="height: 20px; width: 20px; border: 1px solid white; border-radius: 3px; background: white;" readonly="">
        <?= $property->risk_status_name; } ?>
        <?php if($property->property_risk_status==1) { ?>
          <input type="text" name="Color" id="Color" style="height: 20px; width: 20px; border: 1px solid white; border-radius: 3px; background: white;" readonly="">
        <?= $property->risk_status_name; } ?>
        <?php if($property->property_risk_status==2) { ?>
          <input type="text" name="Color" id="Color" style="height: 20px; width: 20px; border: 1px solid green; border-radius: 3px; background: green;" readonly="">
        <?= $property->risk_status_name; } ?>
        <?php if($property->property_risk_status==3) { ?>
          <input type="text" name="Color" id="Color" style="height: 20px; width: 20px; border: 1px solid orange; border-radius: 3px; background: orange;" readonly="">
        <?= $property->risk_status_name; } ?>
        <?php if($property->property_risk_status==4) { ?>
          <input type="text" name="Color" id="Color" style="height: 20px; width: 20px; border: 1px solid red; border-radius: 3px; background: red;" readonly="">
        <?= $property->risk_status_name; } ?>
      </td>
      <td><?= $property->fullName; ?></td>
      <td><?= $property->rooms; ?></td>
      <td>
        <?php if ($property->property_status == 1) { ?>
          <a href="<?= base_url('Property/propertyEdit/'.$property->property_id);?>" style="text-decoration: none;" class="btn btn-info btn-xs" onclick="return confirm('Do You want Edit this Property ?');">Edit</a>
        <?php } elseif ($property->property_status == 2) { ?>
          <a href="<?= base_url('Property/propertyEdit/'.$property->property_id);?>" style="text-decoration: none;" class="btn btn-info btn-xs" onclick="return confirm('Do You want Edit this Property ?');">Edit</a>
        <?php } elseif ($property->property_status == 3) { if ($this->ion_auth->is_admin()) { ?>
          <a href="<?= base_url('Property/propertyEdit/'.$property->property_id);?>" style="text-decoration: none;" class="btn btn-info btn-xs" onclick="return confirm('Do You want Edit this Property ?');">Edit</a>
          <?php } else {
            echo "<b style='color:orange'>Admin</b>";
          }
        } elseif ($property->property_status == 4) { if ($this->ion_auth->is_admin()) { ?>
          <a href="<?= base_url('Property/propertyEdit/'.$property->property_id);?>" style="text-decoration: none;" class="btn btn-info btn-xs" onclick="return confirm('Do You want Edit this Property ???');">Edit</a>
          <?php } else {
            echo "<b style='color:green'>Live</b>";
          }
        } else { if ($this->ion_auth->is_admin()) { ?>
          <a href="<?= base_url('Property/propertyEdit/'.$property->property_id);?>" style="text-decoration: none;" class="btn btn-info btn-xs" onclick="return confirm('Do You want Edit this Property ???');">Edit</a>
          <?php } else {
            echo "<b style='color:red'>Terminated</b>";
          }
        } ?>
      </td>
      <td>
        <a href="<?= base_url('Property/propertyDetails/'.$property->property_id);?>" style="text-decoration: none;" class="btn btn-warning btn-xs">View </a>
        <a href="<?= base_url('Property/viewRoom/'.$property->property_id);?>" style="text-decoration: none;" class="btn btn-success btn-xs">View Rooms</a>  
      </td>
    </tr>
    <?php } } ?>
  </tbody>
</table>
</div>
</div>
</div>
</div>
</div><!--/.row-->
<div class="modal fade" id="myModal">
  <?php include('property_model_view.php'); ?>
</div><!--/.row-->
</div><!--/.row-->

脚本:

<script type="text/javascript">
  function showmore(){
    $.ajax({
        url: "<?= base_url('Property'); ?>",
        data:{
          areaId :$('#areaId').val(),
          cluster_id :$('#cluster_id').val(),
          property_type :$('#property_type').val(),
          property_status :$('#property_status').val()

        },
        type:"JSON", 
        success :function(data){
            $('#example1').prepand(data.view)
            $('#offset').val(data.offset)
        }
    });
  };
</script>

我用谷歌搜索了它,但没有得到正确的结果,欢迎任何形式的帮助。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用Ajax网址:base_url('')site_url('')