我一直在应用程序中工作,该应用程序在个人资料中包含一个喜欢的按钮,我已经在这里停止了,并且不知道如何完成它!为了使它起作用,这里错过了什么?你能帮我吗 ?
阿贾克斯
<script type="text/javascript">
$(document).ready(function() {
$("#likebtn").click(function(e){
$.post(
function(data)
{
if (data.st == 0)
{
$('#likedata').html(data.msg);
}
else if (data.st == 1)
{
<?php echo $numlikes+1; ?>
}
},
'json'
);
return false;
});
});
</script>
视图中的按钮
<p> <input type="button" id="likebtn" class="btn btn-default btn-lg"> Likes</p>
<span class="label label-default" id="Likes"><?php echo $numlikes; ?></span>
<div id="likedata"></div>
控制器的构造功能
function __construct() {
parent::__construct();
$prifile_id = $this->uri->segment(2, 9);
$user_id = ($this->session->userdata['logged_in']['user_id']);
}
控制器功能添加喜欢
public function addlike()
{
$checklike = $this->$this->profiles_model->checklike($user_id,$profile_id);
if ($checklike == FALSE)
{
$this->profiles_model->addlike($user_id,$prifile_id);
$output= array('st'=>1);
echo json_encode($output);
}
else {
$output = array('st'=>0, 'msg' => "you already likes this profile");
echo json_encode($output);
}
}
提前致谢 :)
您想增加计数,但是最好从服务器获取更新的计数。以下是一些javascript更改。
<script>
$.post(
"<?php echo site_url('profiles/addlike'); ?>",
function(data)
{
if (data.st == 0)
{
$('#likedata').html(data.msg);
}
else if (data.st == 1)
{
$("#Likes").text(st.numLikes);
}
},
'json'
);
return false;
});
});
</script>
现在,也从服务器返回更新的numofLikes。
public function addlike()
{
$checklike = $this->$this->profiles_model->checklike($user_id,$profile_id);
if ($checklike == FALSE)
{
// Update this model method, so that it returns updated like count.
$numLikes = $this->profiles_model->addlike($user_id,$prifile_id);
$output= array('st'=>1,'numLikes'=>$numLikes);
echo json_encode($output);
}
else {
$output = array('st'=>0, 'msg' => "you already likes this profile");
echo json_encode($output);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句