php ci ajax提交,php - Codeigniter AJAX Example - Stack Overflow

本文提供了一个新手友好的CodeIgniter AJAX教程,通过创建一个包含输入表单的页面,实现实时数据库插入并根据请求动态加载视图。通过jQuery实现无刷新操作,适用于初学者理解和实践AJAX技术。
摘要由CSDN通过智能技术生成

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.

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

I have this in my 'test' controller:

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

}

}

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

jQuery( document ).ready( function() {

jQuery('#submit').click( function( e ) {

e.preventDefault();

var msg = jQuery('#name').val();

jQuery.post("

test/add", {name: msg}, function( r ) {

console.log(r);

});

});

});

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值