codeigniter AJAX例 [英] Codeigniter AJAX Example

查看:107
本文介绍了codeigniter AJAX例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个星期,现在对于如何使用AJAX与codeigniter一个像样的完整的工作示例(我是一个AJAX新手)。我见过的职位/ TUTS都老了 - 所有的编程语言已经转移

I've been looking for a week now for a decent full working example of how to use AJAX with Codeigniter (I'm an AJAX novice). The posts / tuts I've seen are old - all the programming languages have moved on.

我想,而无需刷新页面有一个页面,该页面返回一些东西到页面(例如,一个变量,数据库查询的结果或HTML格式的字符串)上的输入形式。在这个简单的例子是具有一个输入字段,它插入所述用户输入到数据库中的页面。我想负载不同的看法,一旦输入被提交。如果我能知道如何做到这一点我能适应它做什么,我需要(希望这将帮助别人呢!)

I want to have an input form on a page which returns something to the page (e.g. a variable, database query result or html formatted string) without needing to refresh the page. In this simple example is a page with an input field, which inserts the user input into a database. I'd like to load a different view once the input is submitted. If I could understand how to do this I'd be able to adapt it to do whatever I needed (and hopefully it would help others too!)

我在我的测试控制器:

function add(){
    $name = $this->input->post('name');
    if( $name ) {
        $this->test_model->put( $name );
    }
}

function ajax() {
    $this->view_data["page_title"] = "Ajax Test";
    $this->view_data["page_heading"] = "Ajax Test";

    $data['names'] = $this->test_model->get(); //gets a list of names
    if ( $this->input->is_ajax_request() ) { 
        $this->load->view('test/names_list', $data);
    } else {
        $this->load->view('test/default', $data);
    }
}

下面是我的看法,命名为AJAX(让我访问该通过URL www.mysite.com/test/ajax)

Here is my view, named 'ajax' (so I access this through the URL www.mysite.com/test/ajax)

<script type="text/javascript">
    jQuery( document ).ready( function() {
       jQuery('#submit').click( function( e ) {
           e.preventDefault();
           var msg = jQuery('#name').val();
           jQuery.post("
               <?php echo base_url(); ?>
               test/add", {name: msg}, function( r ) {
                   console.log(r);
               });
           });
       });
</script>

<?php echo form_open("test/add"); ?>
<input type="text" name="name" id="name">
<input type="submit" value="submit" name="submit" id="submit">
<?php echo form_close(); ?>

所有这种情况目前是,I型在一个输入,更新数据库和显示视图测试/默认(它不刷新页面,但根据需要不显示测试/ names_list,许多在此先感谢您的帮助,并把我从我的痛苦!

All that happens currently is that I type in an input, updates the database and displays the view "test/default" (it doesn't refresh the page, but doesn't display "test/names_list" as desired. Many thanks in advance for any help, and for putting me out of my misery!

推荐答案

设置唯一ID的格式为:

Set unique id to the form:

echo form_open('test/add', array('id'=>'testajax'));

我假设你想用视图替换形式:

I assume that you want replace a form with a view:

jQuery(document).ready(function(){
var $=jQuery;
$('#testajax').submit(function(e){
    var $this=$(this);
    var msg = $this.find('#name').val();
    $.post($this.attr('action'), {name: msg}, function(data) {
      $this.replace($(data));
    });
    return false;
});

更好的办法,如果你返回URL的视图,JSON响应:

better way if you return url of view in json response:

$.post("<?php echo base_url(); ?>test/add", {name: msg}, function(data) {
  $this.load(data.url);
},"json");


从最后的评论 - 我强烈不建议更换身体,这将是很难支持这样code


from your last comment - I strongly not suggest to replace body, it will be very hard to support such code.

但这里是雁:

$.post("<?php echo base_url(); ?>test/add", {name: msg}, function(data) {
      $('body').replace(data);
    });

这篇关于codeigniter AJAX例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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