当我按下喜欢的按钮时没有任何反应 你能帮我吗 ? 哪个更好?使它像这样或使它成为一个href按钮 但是href链接到函数。 当我通过网址调用函数时产生的错误
Object of class profiles could not be converted to string
的Ajax
<script type="text/javascript">
$(document).ready(function() {
$("#likebtn").click(function() {
$.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>
查看
<p> <input type="button" id="likebtn" class="btn btn-default btn-lg" value="إعجاب"></p>
<span class="label label-default" id="Likes"><?php echo $numlikes; ?></span>
<div id="likedata"></div>
模型
public function checklike($user_id, $profile_id) {
$condition = "user_id =" . "'" . $user_id . "' AND " . "profile_id =" . "'" . $profile_id . "'";
$this->db->select('*');
$this->db->from('likes');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return TRUE;
} else {
return false;
}
}
public function addlike($user_id, $profile_id) {
$data = array(
'user_id' => $user_id,
'type' => 'profile',
'profile_id' => $profile_id
);
$this->db->insert('likes', $data);
}
public function read_user_information_profile($profile_id) {
$condition = "user_id =" . "'" . $profile_id . "'";
$this->db->select('*');
$this->db->from('users');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
public function likes_profile($profile_id) {
$this->db->count_all();
$this->db->from('likes');
$this->db->where('profile_id', $profile_id);
$query = $this->db->get();
return $query->num_rows();
}
控制器
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class profiles extends CI_Controller {
public $profile_id;
function __construct() {
parent::__construct();
$this->profile_id = $this->uri->segment(2, 9);
$user_id = ($this->session->userdata['logged_in'] ['user_id']);
}
public function index() {
$this->load->model('shoghl_profilat_model');
$data = array();
$result = $this->shoghl_profilat_model->read_user_information_profile($this->profile_id);
$countlike = $this->shoghl_profilat_model->likes_profile($this->profile_id);
$countads = $this->shoghl_profilat_model->ads_profile($this->profile_id);
$level = $this->shoghl_profilat_model->level_profile($this->profile_id);
$data = array(
'username' => $result[0]->username,
'fname' => $result[0]->fname,
'mob' => $result[0]->mob,
'lname' => $result[0]->lname,
'numlikes' => $countlike,
'numads' => $countads,
'levelname' => $level
);
$this->load->view('profiles_view', $data);
}
public function addlike() {
$checklike = $this->$this->shoghl_profilat_model->checklike($user_id, $this->profile_id);
if ($checklike == FALSE) {
$this->shoghl_profilat_model->addlike($user_id, $this->profile_id);
$numLikes = $this->shoghl_profilat_model->likes_profile($this->profile_id);
$output = array('st' => 1);
echo json_encode($output);
} else {
$outputS = array('st' => 0, 'msg' => "you already likes this profile");
echo json_encode($output);
}
}
}
提前致谢
答案 0 :(得分:1)
第一行addlike function
中的控制器存在问题
$checklike = $this->$this->shoghl_profilat_model->checklike($user_id, $this->profile_id);
请注意,$this
出现两次....
应该是
$checklike = $this->shoghl_profilat_model->checklike($user_id, $this->profile_id);
答案 1 :(得分:1)
$.post
操作中使用base_url而不是site_url。$.post
功能中没有为其分配任何值,则不会在您的addlike
请求中发送任何数据。shoghl_profilat_model
函数中加载addlike
如何使用它!$this->profile_id
中的profiles/addlike
不包含任何ID。修复示例:
jQuery aJax代码:
<script type="text/javascript">
$(document).ready(function() {
$("#likebtn").click(function() {
$.post(
"<?=('profiles/addlike/' . $profile_id )?>",
function(data) {
if (data.st == 0) {
$('#likedata').html(data.msg);
} else if (data.st == 1) {
$("#Likes").text(st.numLikes);
}
},
'json'
);
return false;
});
});
</script>
控制器/添加链接功能:
public function addlike ( $profile_id ) {
$this->load->model('shoghl_profilat_model');
$user_id = $this->session->userdata('your_id_key'); // don't forget to change that
$profile_id = (int) $profile_id;
$checklike = $this->$this->shoghl_profilat_model->checklike($user_id, $profile_id);
if ($checklike == FALSE) {
$this->shoghl_profilat_model->addlike($user_id, $this->profile_id);
$numLikes = $this->shoghl_profilat_model->likes_profile($this->profile_id);
$output = array('st' => 1);
echo json_encode($output);
} else {
$outputS = array('st' => 0, 'msg' => "you already likes this profile");
echo json_encode($output);
}
}
答案 2 :(得分:0)
我在Zend应用程序中这样做了。在JQuery部分中,使用
function likedPost(btnId,likeCount){
// May be an AJAX call to increment Like for this post...
$("#likeBtn"+btnId).text(likeCount);
}
但在此之前,您需要更新数据库并获取likeCount的递增值,然后将其传递给此函数。
在HTML中,
<a href="javascript:likedPost(123,55);">Like</a>
希望这有帮助。