Codeigniter jQuery AJAX加载视图页面

3

当我点击锚点时,需要加载控制器函数并从模型中调用数据传递到view_content页面并显示数据。

以下是我的代码:

视图

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

    }

查看内容

<div id="content">
<h3>Welcome to Home Page 12345</h3>
<?php
    print_r($company_data);
?>


那么问题是什么呢..? - Sudhir Bastakoti
仅弹出警告,不加载视图内容页。 - Gihan Wijethunga
请检查您的控制台是否有任何错误,您的URL可能无法正常工作,因为您没有完整的URL...并将您的警报放置在.load()的回调中。 - Sudhir Bastakoti
2个回答

3

由于您正在使用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");
});
...

在你的控制器文件中,
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变量添加到您的站点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");
});

另外一个更新: 如果你想从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");
});

在你的控制器函数中:
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()函数中,您正在尝试加载视图,请更改

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

to

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

这样它就作为字符串返回给您的.load() jQuery函数


我需要打印出site_url中id的值。var id = $(this).attr('id'); $("#loading").load('<?php echo site_url('site2/home2/.+id); ?>'); - Gihan Wijethunga
@GihanWijethunga,请查看更新的答案,您没有正确传递ID。 - Sudhir Bastakoti
我需要将JavaScript变量传递到site_url()中。 ** var id = $(this).attr('id'); $("#loading").load('<?php echo site_url('site2/home2/.'+id+'.); ?>');** 我需要通过这个变量发送id。 @Sudhir - Gihan Wijethunga
1
@GihanWijethunga,您可以将“id”附加到您的site_url中,并创建一个js变量,在.load()中使用它,就像更新后的答案中所示。 - Sudhir Bastakoti
1
你可以像上面的site_url一样从js中传递参数,例如site_url + '/' + id1 + '/' + id2,并在你的控制器函数中添加参数,如function some_function($param1, $param2) { ... }。 - Sudhir Bastakoti
显示剩余2条评论

0

它正在运行

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接