如何将codeigniter与ajax进行网格划分

时间:2013-06-06 19:44:08

标签: php ajax codeigniter

我正在尝试使用代码点火器中的网站实现ajax加一个按钮。我是Ajax和codeigniter的新手,所以我需要一些指导。

这是我正在启动的控制器部分。请注意,这是在创建我的配置文件视图的方法中,我正在尝试创建它。

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

另外这里是为了更好地了解发生了什么,但我希望在加号后面有投票数:

enter image description here

总结一下:基本上我正在学习类似的演示教程的教程,但这并不是我想要做的。这基本上是我从教程中获取代码并尝试将其与我的代码拼接在一起并满足我的特定需求并同时学习ajax。我觉得我很接近,因为它正在更新我的表中的指定列(不正确),但是无法让它明显起作用。

2 个答案:

答案 0 :(得分:0)

我不确定你的问题是什么。但我确实看到了。

尝试获取ajax的视图时,必须将视图作为数据返回

http://ellislab.com/codeigniter/user-guide/general/views.html(页面底部)

喜欢这个

$view = $this->load->view('includes/templates/main_page_template', $data, true);
echo $view;

这会将渲染的视图发送到浏览器

答案 1 :(得分:0)

将此代码放入视图文件

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

然后在脚本中获取隐藏值

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

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