首页 > 解决方案 > CodeIgniter 传输控制器的 HTML 标签/字段以查看 - MVC

问题描述

再会!最近,我的HTML 输入/div被放入 Controller对我来说是个问题。我正在寻找一种解决方案,如何将我的字段/输入传输到视图,以便将它们调用到控制器的视图中。所以我可以轻松地使用我的代码。谢谢你

这是我的控制器- 正如您所看到的那样,它仍然divs存在,所以我只想将它们放到控制器中。

 function fetch()
  {
      $output = '';
      $query = '';
      $this->load->model('level_model');
      if($this->input->post('query'))
      {
       $query = $this->input->post('query');
      }
      $data = $this->level_model->fetch_data($query);
      $output .= '
       <div class="table-responsive">
         <table class="table table-bordered table-striped">
          <tr>
           <th>Courses Year</th>
       </tr>
      ';
      if($data->num_rows() > 0)
      {
       foreach($data->result() as $row)
       {
        $output .= '
          <tr>
           <td>'.$row->course_year.'</td>
          </tr>
        ';
       }
      }
      else
      {
       $output .= '<tr>
           <td colspan="5">No Data Found</td>
          </tr>';
      }
      $output .= '</table>';
      echo $output;
  }

和我的看法

<input type="text" name="search_text" id="search_text" class="form-control">

<div id="result"></div>

阿贾克斯:

 <script>
$(document).ready(function(){

 load_data();

 function load_data(query)
 {
  $.ajax({
   url:"<?php echo base_url(); ?>levels/fetch",
   method:"POST",
   data:{query:query},
   success:function(data){
    $('#result').html(data);
   }
  })
 }

 $('#search_text').change(function(){
  var search = $(this).val();
  if(search != '')
  {
   load_data(search);
  }
  else
  {
   load_data();
  }
 });
});
</script>

<script>

标签: phpjqueryajaxcodeigniter

解决方案


对 HTML 内容使用视图文件。

使用这样的东西

$htmlcontent = $this->load->view('viewfile', $data, true);
echo $htmlcontent;
exit();

推荐阅读