我一直在app中工作,其中包含个人资料中的类似按钮 我已经在这里停下来,不知道如何完成它! 这里有什么错过才能让它发挥作用? 你能帮忙吗?
的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>
视图中的按钮
<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);
}
}
提前致谢:)
答案 0 :(得分:1)
您想增加计数,但最好从服务器获取更新计数。 以下是一些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>
现在,Return也从服务器更新了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);
}
}