Monday, February 25, 2013

Passing values to controller in the Codeigniter using jQuery load

How to use jQuery load() function in Codeigniter?
It took little time to figure out how I can send values together with URL in Codeigniter. After researching little about load jQuery function and Codeigniter, I came to following solution.

Need: I needed to pass the value of attribute when user click a link. It should load the controller method dynamically using jQuery.

Solution:
I used load() method of jQuery in the following manner and pass the id as test_id.

In the Codeigniter controller method test, I grabbed the test_id using Codeigniter's post method,  $this->input->post('test_id'), alternative could be $_POST['test_id']

And my problem is solved.

//pseudocode
$(document).ready(function() {

    $('.link').click(function(){//element to be click to load the page in the div 

    $('#content').load(path, {test_id: $(this).attr("id")});

});

function test(){  

 echo $test_id = $this->input->post('test_id', TRUE);

}

In the view

<div id="content">
</div>


Just wanted to share a simple small bit of pseudo code. May be it will save someones time, if the need is alike mine.

3 comments:

  1. Nice post...I look forward to reading more, and getting a more active part in the talks here, whilst picking up some knowledge as well..

    Pass Box manufacturers

    ReplyDelete