php codeigniter jquery ajax加载视图页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24729255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:26:49  来源:igfitidea点击:

codeigniter jquery ajax load view page

phpjqueryajaxcodeigniter

提问by Gihan Wijethunga

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

回答by Sudhir Bastakoti

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

并在您的控制器文件中,

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

回答by Gihan Wijethunga

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