javascript - 提交按钮上的 AJAX 函数调用在 PHP CodeIgniter 中不起作用

标签 javascript php jquery ajax

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

这是我的代码:

Controller :

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>

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

最佳答案

您可以尝试在 Ajax url:base_url('')site_url('')

关于javascript - 提交按钮上的 AJAX 函数调用在 PHP CodeIgniter 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49023875/

相关文章:

php - mysql_query 在 session 期间不更新数据库

javascript - jQuery add() 方法的奇怪行为

JavaScript 和 iframe 样式

javascript - javascript 中的 if 条件值大于未读入的值

javascript - 将pdf文件的二进制内容保存到javascript变量

javascript - 更改绘图背景颜色

php - 使用 jQuery 效果更改 div 的内容?

javascript - 从 HTML5 Canvas 垂直迭代像素

php - HTML、CSS 导航栏元素通过包含相同文件突出显示,在所有必需页面中包含导航栏

php - 从嵌套大括号中提取代码,包括 PHP 中的多个内部大括号