Home > Backend Development > PHP Tutorial > 如何用Ajax CI实现 关注好友 功能

如何用Ajax CI实现 关注好友 功能

WBOY
Release: 2016-06-06 20:52:09
Original
854 people have browsed it

<div id="follow"><a href="#" class="follow <?php if($selfollow > 0){echo 'message';}?>"><?php if($selfollow == 0){echo '关注';}else{echo '取消关注';}?></a></div>
Copy after login
Copy after login

上面是HTML

$('#follow').click(function(){
if($(".follow").text() == "关注")
{   
    var form_data = {
        pid:<?php echo $selproduct['pid'];?>,
        ajax:'1'
    };

    $.ajax({
        url:"<?php echo site_url('welcome/addfollow/');?>",
        type:'POST',
        data:form_data,
        success:function(msg){
             $('#follow').html();
        }
    });
}
else
{
    var form_data = {
        pid:<?php echo $selproduct['pid'];?>,
        ajax:'1'
    };

    $.ajax({
        url:"<?php echo site_url('welcome/delfollow/');?>",
        type:'POST',
        data:form_data,
        success:function(msg){
            $('#follow').html(msg);
        }
    });

}
return false;
})
Copy after login
Copy after login

上面是我写的jquery代码,功能实现了,但是为什么只有等手动刷新本页面才能看到结果呢?
下面的 CI控制器的写法,求指导。。

function addfollow(){
    $pid = $this->input->post('pid');
    if($this->input->post('ajax')){
    }
}
Copy after login
Copy after login

回复内容:

<div id="follow"><a href="#" class="follow <?php if($selfollow > 0){echo 'message';}?>"><?php if($selfollow == 0){echo '关注';}else{echo '取消关注';}?></a></div>
Copy after login
Copy after login

上面是HTML

$('#follow').click(function(){
if($(".follow").text() == "关注")
{   
    var form_data = {
        pid:<?php echo $selproduct['pid'];?>,
        ajax:'1'
    };

    $.ajax({
        url:"<?php echo site_url('welcome/addfollow/');?>",
        type:'POST',
        data:form_data,
        success:function(msg){
             $('#follow').html();
        }
    });
}
else
{
    var form_data = {
        pid:<?php echo $selproduct['pid'];?>,
        ajax:'1'
    };

    $.ajax({
        url:"<?php echo site_url('welcome/delfollow/');?>",
        type:'POST',
        data:form_data,
        success:function(msg){
            $('#follow').html(msg);
        }
    });

}
return false;
})
Copy after login
Copy after login

上面是我写的jquery代码,功能实现了,但是为什么只有等手动刷新本页面才能看到结果呢?
下面的 CI控制器的写法,求指导。。

function addfollow(){
    $pid = $this->input->post('pid');
    if($this->input->post('ajax')){
    }
}
Copy after login
Copy after login

好吧,问题我自己解决了 要感谢@joyqi 的提示
主要是是操作数据的时候没有返回值, $ajax重写一定要这样写

success:function(msg){

  $('#follow').html(msg);

}
Copy after login

返回的#follow 要单独写一个文件

CI 控制器要这样写:

function addfollow(){


$pid = $this->input->post('pid');


if($this->input->post('ajax')){

$data['pid'] = $pid;

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

}


}
Copy after login

你ajax返回的应该是当前返回数吧,你看看你follow的js代码里

success:function(msg){
             $('#follow').html();
        }
Copy after login

这里应该是$('#follow').html(msg);

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template