codeigniter jquery ajax加载视图页面

时间:2022-10-06 19:25:51

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

当我点击锚标签时我需要加载控制器功能并从模型中的调用函数获取数据并将其发送到view_content页面并显示数据

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);
?>

2 个解决方案

#1


3  

as you are using codeigniter, try changing to:

当您使用codeigniter时,请尝试更改为:

...
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:

更新::将js变量放入您的站点URL,执行:

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:

再多一次更新::如果你想从js向你的函数传递多个参数,你可以这样做:

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

在您的home2()函数中添加::您正在尝试加载视图,更改

$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

以便它作为字符串返回到.load()jquery函数

#2


0  

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");
    });
    });

#1


3  

as you are using codeigniter, try changing to:

当您使用codeigniter时,请尝试更改为:

...
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:

更新::将js变量放入您的站点URL,执行:

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:

再多一次更新::如果你想从js向你的函数传递多个参数,你可以这样做:

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

在您的home2()函数中添加::您正在尝试加载视图,更改

$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

以便它作为字符串返回到.load()jquery函数

#2


0  

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");
    });
    });