[英]Need help in like button in my web app by Codeigniter using ajax
I have been working in app that contains a like button in profiles I have stopped here and don't know how to complete it!! 我一直在应用程序中工作,该应用程序在个人资料中包含一个喜欢的按钮,我已经在这里停止了,不知道如何完成! What is missed here to make it work? 要使它正常工作,这里错过了什么? can you help ? 你能帮我吗 ?
Ajax 阿贾克斯
<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>
Button in the View 视图中的按钮
<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>
on construct function of the controller 控制器的构造函数
function __construct() {
parent::__construct();
$prifile_id = $this->uri->segment(2, 9);
$user_id = ($this->session->userdata['logged_in']['user_id']);
}
Controller function to add like 控制器功能添加喜欢
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);
}
}
Thanks in advance :) 提前致谢 :)
You want to increment count, but it is good to get updated count from server. 您想增加计数,但是最好从服务器获取更新计数。 Following is some javascript changes. 以下是一些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>
Now, Return updated numofLikes from server too. 现在,也从服务器返回更新的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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.