douye9822 2014-07-10 06:29
浏览 71

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

I need to change only my webpage content without refreshing the page,

view

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

this link is on my nav bar,when I click this link I need to change content, there need to pass id to my controller,

controller

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

and data in the result array should be view in the view content,I need ajax soution

  • 写回答

1条回答 默认 最新

  • dongwen7187 2014-07-10 06:50
    关注

    Your anchor

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

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

    here's the 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);
          }
    
         })
      })
    })
    
    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染