dqbn76906 2014-01-12 10:47
浏览 49

如何在没有页面重新加载的情况下使用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.

  • 写回答

1条回答 默认 最新

  • dongzhan5943 2014-01-12 12:18
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等