Jquery 尝试在codeigniter中制作一个类似的按钮
当我按下按钮时,什么也没发生 你能帮忙吗? 哪一个更好?这样做还是让它成为一个href按钮 但href链接指向该函数。 当我通过url调用函数时,会产生一个错误Jquery 尝试在codeigniter中制作一个类似的按钮,jquery,ajax,codeigniter,Jquery,Ajax,Codeigniter,当我按下按钮时,什么也没发生 你能帮忙吗? 哪一个更好?这样做还是让它成为一个href按钮 但href链接指向该函数。 当我通过url调用函数时,会产生一个错误 Object of class profiles could not be converted to string Ajax <script type="text/javascript"> $(document).ready(function() { $("#likebtn").click(function(
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>
控制器
<?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);
}
}
}
我在一个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>
希望这有帮助。第一行的addlike函数中的控制器有问题
$checklike = $this->$this->shoghl_profilat_model->checklike($user_id, $this->profile_id);
请注意,$this
出现两次
应该是
$checklike = $this->shoghl_profilat_model->checklike($user_id, $this->profile_id);
- 首先,您应该在
$操作中使用基本url而不是站点url
- 您没有在like
$中发送任何数据。在addlike
函数中使用用户id时,如果未为其分配任何值,请发布请求
- 在addlike函数的第一行重复$this两次
- 您没有在
addlike
函数中加载shoghl\u profilat\u模型
,您如何使用它李>
- 在配置文件控制器构造中,
$this->profile\u 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>
错误表示,$numlikes
不是字符串,而是对象。您可以使用var_dump($this->shoghl_profilat_model->likes_profile($this->profile_id))检查它;退出代码>在控制器中加载模型后,查看输出内容。@Tpojka输出为->int 1d是否在输入字段周围有包含操作的表单?不,只有按钮@TpojkaSession userdata是需要参数的函数。我认为构造函数中的这部分代码是不正确的:$this->session->userdata['logged\u in']['user\u id']
,应该类似于$this->session->userdata('item')
。检查。谢谢,但当我按下按钮时,什么也没发生:(您是否仍然收到此错误类配置文件的对象无法转换为字符串
?否,现在消息:未定义属性:配置文件:$shoghl\u profilat\u model能否根据当前情况更新代码?另外,请在运行时为模型添加类名行…我不明白您的意思?我的问题是当我按下like按钮时,它不工作
$checklike = $this->shoghl_profilat_model->checklike($user_id, $this->profile_id);
<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);
}
}