0

How can to add some ajax to load my view part? I'm done already with the delete/add and update Ajax.

Now I don't know how to load the data without refreshing the browser. Every time I do the delete/add and update I need to refresh my browser first to see the result. I know it will solve the problem by having auto load data from database without refreshing the browser.

Model

public function getManufacturerRecord(){ //view data
    $this->db->select('*')
             ->from('manufacturer');
    $query = $this->db->get();
    return $result = $query->result();
}

Controller

public function manufacturer_list(){
    if($this->session->userdata('is_logged_in')){

        $result['data'] = $this->manufacturer_model->getManufacturerRecord();

        $this->load->view('../template/header');
        $this->load->view('manufacturer', $result);
        $this->load->view('../template/footer');
    } else {
        redirect('main/restricted');
    }
}

View

<div id="navscroll">
    <table class="table-condensed table-bordered table-striped table-hover">
        <thead>
            <tr>
                <th>ID</th>
                <th>Manufacturer</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php if(!empty($data)): 
            foreach($data as $row) { ?>
            <tr>
                <td><?=$row->id;?></td>
                <td><?=$row->brand;?></td>
            </tr>
            <?php } else : ?>
            <tr>No Records Found!</tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>
</div>
2
  • Can you come to the chat. I wish to solve your problem but I need more information about your work. click the link chat.stackoverflow.com/rooms/137124/codeigniter Commented Mar 6, 2017 at 13:09
  • Hi I'm already in the chat Sir. Commented Mar 6, 2017 at 13:23

2 Answers 2

2

Following code will help you to sort your problem

Few Changes in View Page

add table id as id="manu-table" and in anchor() function and replace it as follow

<?=anchor("manufacturer/delete/{$row->id}","Delete", array('class'=>'btnDelete', 'data-id' => $row->id))?>

For Add Function

Replace your existion form_open() function as bellow

<?=form_open('manufacturer/add',array('id' => 'manifu_add_form'));?> 

and submit function as follow

<?=form_button(['content'=>'Save', 'type' => 'submit', 'class'=>'btn btn-primary', 'id'=>'btnSubmit'])?>

Add Ajax Script

var site_url = "http://localhost/ci_attl/manufacturer/";
$("#manifu_add_form").submit(function(e){ 
    e.preventDefault(); 
    var manufacturer = $("#manufacturer").val(); 
    $.ajax({ 
        url: $(this).attr('action'), 
        method: "POST", 
        data: { manufacturer : manufacturer }, 
        success: function(data){ 
        console.log(data); 
        var obj = $.parseJSON(data); 
        if(obj.status == 'success') 
        { 
            var append = "<tr id='row-"+obj.last_id+"'><td>"+obj.last_id+"</td><td>"+manufacturer+"</td><td><a data-toggle='modal' href='#update"+obj.last_id+"'>Update</a>| <a href='"+site_url+obj.last_id+"'>Delete</a></td></tr>";
            console.log(append);
            $("#manu-table tbody").append(append); 
            $('#manifu_add_form')[0].reset(); 
        } 
        $("div.result").html(obj.msg); 
        }, 
        error:function(a,b,c) 
        { 
            alert(c); 
        } 
    }); 
});

For Delete Function

change your anchor() as follow

<?=anchor("manufacturer/delete/{$row->id}","Delete", array('class'=>'btnDelete', 'data-id' => $row->id))?> 

and delete Ajax as follow

$('.btnDelete').click(function(){ //delete data 
    var a_href = $(this).attr('href'); 
    var id = $(this).data('id'); 
    $.ajax({ 
        type: "GET", 
        url: a_href, 
        success: function(data){ 
            $("#row-"+id).slideUp(); 
            $("div.result").html(data); 
        }, 
        error:function(a,b,c){ 
            console.log(c); 
        } 
    }); 
    return false 
});

Hope this will solve your issue

Sign up to request clarification or add additional context in comments.

3 Comments

This is very helpful, but why is the JavaScript not indented? I was going to +1, but this is not teaching good practice!
@halfer I solve this in chat.stackoverflow.webpage. So I paste the code directly from there it self. That's why i t not indent the code. Hope you know that, in chat code formatting will be removed and contents will rearrange to same alignments. I agree with what you have said. Now I correct the code.
I don't agree that is a reason for pasting it unindented here, but thank you for repairing it.
0

Why not use a redirect to that page after the delete,update,add.

with route:

$route['temp'] = 'controller/function';

redirect(base_url('temp'));

without route:

redirect(base_url('controller/function'));

Comments

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.