【问题标题】:codeigniter jquery ajax load view pagecodeigniter jquery ajax 加载视图页面
【发布时间】:2014-07-14 03:47:31
【问题描述】:

当我点击锚标签时我需要加载控制器函数并从模型中的调用函数中获取数据并将其发送到 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();

    }

view_content

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

【问题讨论】:

  • 那是什么问题..?
  • 不加载 view_content 页面,仅工作警告
  • 检查您的控制台是否有任何错误,您的 url 可能无法正常工作,因为您没有完整的 url ...并将您的警报放在 .load() 的回调中

标签: php jquery ajax codeigniter


【解决方案1】:

当您使用 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");
});
...

在您的 controller 文件中,

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

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

使其作为字符串返回到您的.load() jquery 函数

【讨论】:

  • 我需要在 site_url 中打印 id 的值 var id = $(this).attr('id'); $("#loading").load('');
  • @GihanWijethunga 查看更新的答案,您没有正确传递 id
  • 我需要将 javascript 变量放入 site_url() ** var id = $(this).attr('id'); $("#loading").load('');** 我需要通过变量@Sudhir发送id >
  • @GihanWijethunga 您可以将“id”附加到您的 site_url,并创建一个 js 变量并在 .load() 中使用它,如更新的答案中所示
  • 您可以像上面的 site_url 中所做的那样从 js 中传递参数,如 site_url + '/' + id1 + '/' + id2,并向控制器函数添加参数,如 function some_function($param1, $param2) { ... }
【解决方案2】:

它的工作原理

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多