Php 如何将codeigniter与ajax结合起来
我试图在CodeIgniter中用我的站点实现一个ajax加一个按钮。我是Ajax和codeigniter的新手,所以我需要一些指导 这是我的控制器的开始部分。请注意,这在创建我的纵断面图的方法中,我正试图在其中创建此纵断面图Php 如何将codeigniter与ajax结合起来,php,ajax,codeigniter,Php,Ajax,Codeigniter,我试图在CodeIgniter中用我的站点实现一个ajax加一个按钮。我是Ajax和codeigniter的新手,所以我需要一些指导 这是我的控制器的开始部分。请注意,这在创建我的纵断面图的方法中,我正试图在其中创建此纵断面图 $voteId= $this->input->post('voteId'); $upOrDown= $this->input->post('upOrDown');
$voteId= $this->input->post('voteId');
$upOrDown= $this->input->post('upOrDown');
$status ="false";
$updateRecords = 0;
if($upOrDown=='upvote'){
$updateRecords = $this->community_model->updateUpVote($voteId);
}else{
$updateRecords = $this->community_model->updateUpVote($voteId);
}
if($updateRecords>0){
$status = "true";
}
echo $status;
// This variable will be accessed from the view
$data['airwave'] = $airwave;
$data['profile_id'] = $profile_id;
$this->load->view('includes/templates/main_page_template', $data);
}
以下是模型中的方法:
function updateUpVote($voteId){
$sql = "UPDATE airwaves_comments set thumbsup = thumbsup+1 WHERE thumbsup =?";
$this->db->query($sql, array($voteId));
return $this->db->affected_rows();
}
以下是我的看法:
<div id='thumb_holder'>
<div id='thumb_report'>
<a href='mailto:info@cysticlife.org'>
• report
</a>
</div>
<div class= 'thumb_counter'>
+0
</div>
<div id='thumb_thumb'>
<a class='myButtonLink voteMe' id='1_upvote'<span id="1_upvote_result">+</span>></a>
</div>
</div>
+0
以下是脚本:
<script>
$(document).ready(function(){
$(".voteMe").click(function() {
var voteId = this.id;
var upOrDown = voteId.split('_');
$.ajax({
type: "post",
url: "account/profile/voteMe",
cache: false,
data:'voteId='+upOrDown[0] + '&upOrDown=' +upOrDown[1],
success: function(response){
try{
if(response=='true'){
var newValue = parseInt($("#"+voteId+'_result').text()) + 1;
$("#"+voteId+'_result').html(newValue);
}else{
alert('Sorry Unable to update..');
}
}catch(e) {
alert('Exception while request..');
}
},
error: function(){
alert('Error while request..');
}
});
});
});
</script>
$(文档).ready(函数(){
$(“.voteMe”)。单击(函数(){
var voteId=this.id;
var upOrDown=voteId.split(“"”);
$.ajax({
类型:“post”,
url:“帐户/个人资料/voteMe”,
cache:false,
数据:'voteId='+upOrDown[0]+'&upOrDown='+upOrDown[1],
成功:功能(响应){
试一试{
如果(响应=='true'){
var newValue=parseInt($(“#”+voteId+“#u result”).text())+1;
$(“#”+voteId+“#u result').html(newValue);
}否则{
警报('抱歉,无法更新..');
}
}第(e)款{
警报(“请求时出现异常…”;
}
},
错误:函数(){
警报(“请求时出错…”;
}
});
});
});
此外,这是一个更好地了解情况的外观,但我希望在加号后有投票数:
总而言之:本质上,我是在一个类似的演示教程上学习一个教程,但这并不是我想要做的。这基本上是我从教程中获取代码,并尝试将其与我的代码拼接在一起,满足我的特定需求,同时学习ajax。我感觉它正在更新我的表中的指定列(错误地),但却无法使其正常运行 我不太清楚你说你的问题是什么。但我确实看到了这一点 尝试获取ajax视图时,必须将视图作为数据返回 (页面底部) 像这样
$view = $this->load->view('includes/templates/main_page_template', $data, true);
echo $view;
这将向浏览器发送渲染视图将此代码放入视图文件中
<input type="hidden" name="siteurl" id="siteurl" value="<?php echo site_url(); ?>">
<input type="hidden" name="baseurl" id="baseurl" value="<?php echo base_url(); ?>">
你的问题是什么facing@dianuj查看我的编辑,谢谢。我正在尝试在单击竖起大拇指的图像时更新计数器(比如现在的+0),并在每次单击时增加它,并将这些“竖起大拇指”的增量保存在我为其创建的列中,该列分配给我竖起大拇指的特定注释。
var site_path = $("#siteurl").val();
var base_path = $("#baseurl").val();
$.ajax({
type: "post",
url: site_path+"account/profile/voteMe",....