0

I have write a some codeigniter code, when I click button add a data, and the table will ajax reload, not reload the page. How to modify my code? Thanks

This is my controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Big_category extends CI_Controller {

  function __construct() {
     ...
  }

  function index() {
    if($this->session->userdata('logged_in')) {
      $this->page(0);
    } else {
      show_404('page');
    }
  }

  function page($page) {
     ....
    $data['pagelist']=$this->pagination->create_links();
    $data["main_content"] = "big_category_view";
    $this->load->view('template', $data);
  }

This is my model

class Big_category_model extends CI_Model
{
  ...
  function get_big_category($limit, $start)
  {
    $this->db->limit($limit, $start);
    $query = $this->db->get_where('category1', array('c1_status' => 1)); //SELECT * FROM category1
    return json_encode($query->result());
  }

This is my view

...
    <? $data = json_decode($all_big_category); ?>
    <? foreach($data as $key => $value): ?>
    <tr class="modify_tr">
      <td><?=($key+1)?></td>
      <td id="td_id_<?=$value->c1_id?>" class="modify_td">

        <span id="c1_id_<?=$value->c1_id?>"><?=$value->c1_name?></span>
        <form class="form-search td_id_<?=$value->c1_id?>">
          <input type="text" name="input_c1_id" id="input_c1_id_<?=$value->c1_id?>">
          <button class="btn modify" id="button_c1_id_<?=$value->c1_id?>" c1-id="<?=$value->c1_id?>" type="button"><i class="icon-edit"></i></button></td>
        </form>
      <td><button class="btn btn-danger"><i class="icon-remove-sign"></i></button></td>
    </tr>
    <? endforeach ?>
  </table>
  <?=$pagelist?>
...
2
  • What's not working? Where is your ajax call? What is your expected result vs. what is happening right now? Commented Mar 15, 2013 at 4:26
  • The view is had write some table code, and if I want use ajax load table, that I should write this code again? I want the view code become a template, the data will fill in this table, I have no idea, could you give me few pointing? Thanks Commented Mar 15, 2013 at 5:03

1 Answer 1

3

Here is a simple technique of how you can achieve this.

Layout(Template)

<html>
<body>
    <div><!--other stuff here--></div>
    <div id = 'form_id'>
    <?php echo $content?>
    </div>
</body>
</html>

Controller

function index() 
{
    if($this->input->is_ajax_request()){
        //load form here
        $this->load->view('form');      
    }else{
        //this will load the form for the first time
        //pass third parameter as TRUE so it will returned as string
        //assigned to index content which will be displayed in layout
        $data['content']    =   $this->load->view('form',$data , TRUE);     

        //load template here
        $this->load->view('template', $data);       
    }
}

JQuery

$.ajax({
    type : 'POST',
    url : '<?php echo site_url('controllername/index')?>',
    data : '' // query string
    success : function(formagain){
        $('#form_id').html(formagain);
        //this will replace the content of div with new form
    } 
});
Sign up to request clarification or add additional context in comments.

2 Comments

I had a doubt, the ajax's $data , I would use a lof string to build a form?(like this: var str = "<form><table><tr><td>...."). Thanks.
@Lighter writing php in html is not a good practice and when CI is giving to seperate view than why dont we use it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.