如何使用Ajax使Codeigniter网格化 [英] How to mesh codeigniter with ajax

查看:41
本文介绍了如何使用Ajax使Codeigniter网格化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在代码点火器中的网站上实现一个Ajax加一个按钮.我对Ajax和Codeigniter还是很陌生,所以我需要一些指导.

I am trying to implement a ajax plus one button with my site in code igniter. I am very new with Ajax and codeigniter so I'm needing some guidance.

这是我控制器中开始的部分.请注意,这是在创建我的个人资料视图的方法中进行的.

Here is the portion of my controller where I'm starting. Please note, this is inside the method that creates my profile view in which I'm trying to create this.

$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'>
        &#149 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>

这是脚本:

<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>

另外,这里是为了更好地了解正在发生的事情的样子,但是我希望加号后面有投票数:

additionally here is what it looks like to give a better idea of what's going on, but I want there to be the number of votes after the plus sign:

总结:本质上,我正在学习一个与演示教程类似的教程,但这并不是我要尝试做的.基本上,这就是我从教程中获取代码,并尝试将其与我的代码拼接起来,以满足我的特定需求并同时学习Ajax.我觉得我快要关闭了,因为它正在更新表中的指定列(不正确),但是却无法使其正常运行.

To summarize: Essentially I was taking a tutorial on a demo tutorial that is similar, but it was not exactly what I am trying to do. This is basically me taking the code from the tutorial and trying to splice it with my code and meet my specific needs and learning ajax at the same time. I feel like I'm close as it's updating the specified column in my table (incorrectly),however can't get it to visibly function.

推荐答案

将此代码放入视图文件

    <input type="hidden" name="siteurl" id="siteurl" value="<?php echo site_url(); ?>">
    <input type="hidden" name="baseurl" id="baseurl" value="<?php echo base_url(); ?>">

然后在脚本中获取隐藏的值

Then get hidden value in your script

var site_path = $("#siteurl").val();    
var base_path = $("#baseurl").val();

 $.ajax({
                    type: "post",
                    url: site_path+"account/profile/voteMe",....

这篇关于如何使用Ajax使Codeigniter网格化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆