codeigniter jQuery的AJAX加载视图页面视图、加载、页面、codeigniter

2023-09-10 20:29:30 作者:您太賤忘了°

我需要当我点击锚标记,我需要负载控制器的功能和调用函数模型中获取数据,并将其发送给view_content页和显示数据。

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

这是我的code

查看

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

控制器

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

    }

模式

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

推荐答案

由于您使用的是codeigniter,尝试更改为:

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

和您的控制器文件,

function some_function($your_id) {
    //get data
    //do something with $your_id
    //pass view as string
    echo $this->load->view('view_content', $data, TRUE);
}

更新:: 把JS变量在您的网站的网址,这样做:

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

多了一个更新:: 如果你想多个参数从JS传递给你的函数,你可以这样做:

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

和你的控制器功能

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

此外:: 在 HOME2()函数你要加载查看,更改

addition:: in your home2() function your are trying to load view, change

$this->load->view('view_content',$data);

echo $this->load->view('view_content',$data, TRUE);

,以便其返回的字符串到你的 .load() jQuery函数

so that its returned as string to your .load() jquery function