Reputation: 85
I need when I click anchor tag I need load controller function and get data from calling function in the model and send it to view_content page and display data
here is my code
view
<div id="loading"></div>
<a href="" class="company">Click Here</a>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".company").click(function(e){
e.preventDefault();
var id = $(this).attr('id');
var site_url = "<?php echo site_url('site2/home2/'); ?>" +id; //append id at end
$("#loading").load(site_url);
//alert(id);
//alert("aawa");
});
});
</script>
controller
public function home2($id){
$this->load->model('get_click_data');
$data['company_data'] = $this->get_click_data->get_company($id);
$this->load->view('view_content',$data);
}
model
<?php
class Get_click_data extends CI_Model{
public function get_company($id){
$query = $this->db->query("SELECT * from companydetails where id = '$id'");
return $query->result();
}
view_content
<div id="content">
<h3>Welcome to Home Page 12345</h3>
<?php
print_r($company_data);
?>
Upvotes: 3
Views: 23725
Reputation: 100175
as you are using codeigniter, try changing to:
...
var id = $(this).attr('id'); //you need to have 'id' attribute in your anchor
$("#loading").load("<?php echo site_url('site2/home2/); ?>" + id, function() {
alert("aawa");
});
...
and in your controller file,
function some_function($your_id) {
//get data
//do something with $your_id
//pass view as string
echo $this->load->view('view_content', $data, TRUE);
}
Update:: to put js variable in your site url, do:
var id = $(this).attr('id');
var site_url = "<?php echo site_url('site2/home2/'); ?>" + id; //append id at end
$("#loading").load(site_url, function() {
alert("aawa");
});
one more update:: if you want to pass multiple parameters to your function from js, you can do:
var id = $(this).attr('id');
var second_param = "some value here";
var site_url = "<?php echo site_url('site2/home2/'); ?>" + id + "/" + second_param;
$("#loading").load(site_url, function() {
alert("aawa");
});
and in your controller function
function some_function($your_id, $other_param) {
do something with $your_id and $other_param
//pass view as string
echo $this->load->view('view_content', $data, TRUE);
}
addition::
in your home2()
function your are trying to load view, change
$this->load->view('view_content',$data);
to
echo $this->load->view('view_content',$data, TRUE);
so that its returned as string to your .load()
jquery function
Upvotes: 3
Reputation: 85
its working
<script type="text/javascript">
$(document).ready(function(){
$(".company").click(function(e){
e.preventDefault();
var id = $(this).attr('id');
$("#loading").load('<?php echo site_url('site2/home2/'); ?>'+id);
alert(id);
//alert("aawa");
});
});
Upvotes: 0