简体   繁体   中英

how to post and get data using php and codeigniter without page reload

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM