codeigniter通过单击锚点更改内容而不刷新页面

时间:2014-07-10 06:29:59

标签: javascript php jquery ajax codeigniter

我需要更改我的网页内容而不刷新页面,

查看

<a href="<?php echo site_url('site2/getBranchDetails/'.$row_bank->branch_id.''); ?>"><?php echo $row_bank->bank;  ?></a>

此链接在我的导航栏上,当我点击此链接时我需要更改内容,需要将ID传递给我的控制器,

控制器

 public function getBranchDetails($b_id){
        $this->load->model('bank_account_model');
        $data['results'] = $this->bank_account_model->getAccount($b_id);
    }

结果数组中的数据应该在视图内容中查看,我需要ajax soution

1 个答案:

答案 0 :(得分:0)

你的主播

<a href="javascript:;" class="branch" alt="123">CLick me</a>

PHP

public function getBranchDetails(){

    $b_id = $this->input->post('branch_id');
    $this->load->model('bank_account_model');
    $data = $this->bank_account_model->getAccount($b_id); //suppose its ->result();

    $rows  ='<ul>'; //i use ul, use table if u want
    foreach($data as $r){

        $rows .='<li>'.$r->branch_city.'</li>';
        $rows .='<li>'.$r->branch_name.'</li>';
    }
    $rows .='</ul>';


   echo json_encode(array('data'=>$rows));
}

这里是jquery / ajax

$(function(){

 $(".branch").click(function(){

    var branch_id = $(this).attr("alt");

    $.ajax({

     url : 'your_url/getBranchDetails',type:'post',dataType:'json',
     data: {branch_id:branch_id},
     success:function(result){

        //fetch result.data
         alert(result.data); //do it yourself from here 
         //(append it somewhere)
         $('body').append(result.data);
      }

     })
  })
})