如何在没有页面重新加载的情况下使用php和codeigniter发布和获取数据

I am beginner in codeigniter MVC.

Question: How to send and receive data using codeigniter and PHP without page refresh or reload.

NOTE: Please give some MVC examples using either ajax or jquery or both.

There are many tutorials in google about this. But here is the simple example using CodeIgniter and Jquery:

  1. Create a file named test_controller.php in your controller folder. Put this code:

    <?php
    class Test_controller extends CI_Controller {
            public function index() 
            {
                    $this->load->view("test_view"); 
            }
    
            public function do_ajax()
            {
                    $name=$this->input->post("name"); //get posted data
                    echo "Hello $name, I am AJAX"; //return response
            }
    
    }
    
  2. Create a file named test_view in your View folder. Put this code:

    <html>
            <head>
                    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
                    <script>
                            function getpost_ajax() {
                                    var name=$("#name").val();
    
                                    //begin ajax
                                    $.post("<?php echo site_url('stack/test_controller/do_ajax'); ?>", { 
                                            //data to post
                                            name:name
                                    }, function(data){
                                            //on success, alert the data
                                            alert(data)                                                        
                                    });
                            }
                    </script>
            </head>
            <body>
                    <input type="text" id="name" />
                    <input type="button" onclick="getpost_ajax()" value="GO!"/>
            </body>
    </html>
    
  3. Open in your browser: http://localhost/your_site_folder/index.php/test_controller. Fill the textbox with any data, and then click GO!. It will post the data from the textbox, and alert the response.