【问题标题】:How to process a form with CodeIgniter如何使用 CodeIgniter 处理表单
【发布时间】:2010-02-17 08:43:59
【问题描述】:

我是 CodeIgniter 的新手。我需要处理一个表格。我有一个 form.html 页面在视图中

<html>
  <head>
    <title>Search</title>
  </head>
  <body>
    <form action="search">
      <input type="text" name="search" value="" size="50" />
      <div>
        <input type="submit" value="Submit" />
      </div>
    </form>
  </body>
</html>

和表单控制器

class Form extends Controller {

  function Form() {
    parent::Controller();   
  }

  function index() {    
    $this->load->view('form');
  }

}

我有一个视图文件 search.php,但是当它被处理时它显示页面未找到...

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    M.odel V.iew C.ontroller 设置(如 CodeIgniter)中,视图是用户界面元素。他们不应该解析结果。

    如果我没记错的话,您要做的是将数据从 www.yoursite.com/index.php/form 传递到 www.yoursite.com/index.php/search

    在非结构化 php 中,您可能有一个 form.html,其表单操作为 search.php。用户将导航到yoursite.com/form.html,它会调用yoursite.com/search.php,它可能会重定向到yoursite.com/results.php

    在 CodeIgniter 中(据我所知,在任何 MVC 系统中,无论语言如何),您的 ControllerForm 调用一个将 form.html View 加载到自身的函数 /strong> 然后运行它。 View 生成用户与之交互的代码(通常是 HTML,但不一定)。当用户发出 View 无法处理的请求(请求更多数据或另一个页面)时,它会将该请求传递回 Controller,后者会加载更多数据或另一个 View。

    换句话说,视图决定了数据的显示方式。控制器将请求映射到视图。

    当您想要在视图中显示复杂和/或变化的数据时,它会稍微复杂一些。为了维护MVC需要的separation of concerns,CodeIgniter还为你提供了Models

    模型负责任何 Web 应用程序中最困难的部分 - 管理数据流。它们包含读取数据、写入数据的方法,最重要的是,确保数据完整性的方法。换句话说,模型应该:

    • 确保数据格式正确。
    • 确保数据不包含任何可能破坏其目标环境的内容(恶意或其他)。
    • 拥有C.reating、R.eading、U.pdating和D.eleting的方法上述限制内的数据。

    Akelos 有一个很好的图形来布置 MVC 的组件:


    (来源:akelos.org

    话虽如此,完成您想做的最简单(读作“最简单”,而不是“最可扩展”)的方法是:

    function Form()
    {
        parent::Controller();   
    }
    
    function index()
    {   
            $this->load->view('form');
    }
    
    function search()
    {
            $term = $this->input->post('search');
            /*
                In order for this to work you will need to 
                change the method on your form.
                (Since you do not specify a method in your form, 
                it will default to the *get* method -- and CodeIgniter
                destroys the $_GET variable unless you change its 
                default settings.)
    
                The *action* your form needs to have is
                index.php/form/search/
            */
    
            // Operate on your search data here.
            // One possible way to do this:
            $this->load->model('search_model');
            $results_from_search = $this->search->find_data($term);
    
            // Make sure your model properly escapes incoming data.
            $this->load->view('results', $results_from_search);
    }
    

    【讨论】:

    • +1 -- 我的乐观支持。它清晰而诱人。我不够熟练,无法评估它的真实性,但我会看看我是否可以用它来变得如此。
    • $term = $this-&gt;input-&gt;post('search'); here ... 'search' 是表单的名称,对吗?
    • @b_dubb - search字段的名称&lt;input type="text" name="search"&gt;
    【解决方案2】:

    没有控制器加载和显示视图文件是无用的。您必须创建一个控制器来接收表单数据,对其进行处理,然后显示处理结果。

    您可以使用表单助手来设置表单打开标签,也可以设置关闭标签:

    <?php echo form_open('form/search'); ?>
    <input type="text" name="search" value="" size="50" />
    <div><input type="submit" value="Submit" /></div>
    <?php echo form_close(); ?>
    

    不使用表单助手,你仍然可以这样写:

    <form action="<?php echo site_url('form/search'); ?>">
    

    然后将search方法添加到form控制器中:

    function search()
    {
      //get form field
      $search = $this->input->post('search');
      // do stuffs here
      //...
    }
    

    请记住,CI 仅帮助您进行基本的代码组织,并提供有用的库和帮助程序。但是您仍然需要在您的站点中编写该过程的算法。

    不要忘记阅读下载的 codeigniter 包中包含的用户指南。您可以从那里的示例中学到很多东西。不知道的地方不要犹豫,stackoverflow 的很多成员都会帮到你。

    【讨论】:

      【解决方案3】:

      这是表单验证并在控制器中提交 我的整个控制器类

          class MY_Controller extends CI_Controller {
      
              function __construct()
              {
                  parent::__construct();
      
                  $this->load->library(array('session','form_validation'));
                  $this->load->helper(array('form', 'url', 'date'));
      
                  //$this->load->config('app', TRUE);
      
                  //$this->data['app'] = $this->config->item('app');
      
      
                  }
          }
      
          <?php
      
          if (!defined('BASEPATH'))
              exit('No direct script access allowed');
      
          class Article extends MY_Controller {
      
              function __construct() {
                  parent::__construct();
                  $this->load->model('article_model');
              }
      
              public function index() {
      
                  $data['allArticles']    =   $this->article_model->getAll(); 
      
                  $data['content']        =   $this->load->view('article', $data, true);
                  $this->load->view('layout', $data);
      
              }
      
              public function displayAll() {
      
                  $data['allArticles']    =   $this->article_model->getAll(); 
      
                  $data['content']        =   $this->load->view('displayAllArticles', $data, true);
                  $this->load->view('layout', $data);
      
              }
      
              public function displayArticle($id) {
      
                  $data['article']        =   $this->article_model->read($id); 
      
                  $data['articleId']      =   $id;
      
                  $data['comment']        =   $this->load->view('addComment', $data, true);
      
                  $data['content']        =   $this->load->view('displayArticle', $data, true);
      
      
                  $this->load->view('layout', $data);
      
              }
      
              public function add() {
      
                  $this->form_validation->set_message('required', '%s is required');
                  $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
                  $this->form_validation->set_rules('description', 'Description type', 'required|xss_clean');
      
                  $this->form_validation->set_error_delimiters('<p class="alert alert-danger"><a class="close" data-dismiss="alert" href="#">&times;</a>', '</p>');
      
      
                  if ($this->form_validation->run() == TRUE) {
      
                       $article = array(
                              'title'         => $this->input->post('title'),
                              'description'   => $this->input->post('description'),
                              'created'       => date("Y-m-d H:i:s")
                        );
      
                       $this->article_model->create($article);
      
                       redirect('article', 'refresh');
      
      
                  } else {
      
                       $data['article'] = array(
                          'title'         => $this->input->post('title'),
                          'description'   => $this->input->post('description'),
                      );
      
                      $data['message'] = validation_errors();
      
                      $data['content'] = $this->load->view('addArticle', $data, true);
                      $this->load->view('layout', $data);
                  }
              }
      
          }
      

      我们可以像这样使用普通的html表单。

                  <?php echo $message; ?>
      
                 <form method="post" action="article/add" id="article" >
                      <div class="form-group">
                          <label for="title">Article Title</label>
                          <input type="text" class="form-control" id="title" name="title" value="<?php echo $article['title']; ?>" >
                      </div>
                      <div class="form-group">
                          <label for="description">Description</label>
                          <textarea class="form-control" rows="13" name="description" id="description"><?php echo $article['description']; ?></textarea>
                      </div>
      
                      <button type="submit" class="btn btn-default">Submit</button>
                  </form>     
              </div>
          </div>
      

      【讨论】:

        【解决方案4】:

        尝试在您的操作中使用 codeigniter 'site_url' 以确保您指向正确的位置。您示例中的操作将转到“搜索”控制器而不是“表单”控制器。

        <html>
        <head>
        <title>Search</title>
        </head>
        <body>
        <form action="<?= site_url('form/process_search') ?>">
        <input type="text" name="search" value="" size="50" />
        <div><input type="submit" value="Submit" /></div>
        </form>
        </body>
        </html>
        

        index 仅在没有传递其他任何内容时在您的控制器中使用。所以在我上面的示例中,您需要这样的东西:

        function Form()
        {
            parent::Controller();   
        }
        
        function process_search()
        {   
        
             print "<pre>";
        
             print_r($_POST);
        
             print "</pre>";
        }
        

        【讨论】:

          【解决方案5】:

          Nettuts 有一个很棒的 CodeIgniter for Login 教程。关注截屏视频,它将解决您的问题。

          http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-6-login/

          【讨论】:

          • 链接已失效。悲伤的脸。
          【解决方案6】:

          &lt;?php echo form_open('form/search');?&gt;替换这个&lt;form action="search"&gt; 和 autoload.php 文件添加$autoload['helper'] = array('form');

          然后文件不要使用文件 search.php 只需将您的 search.php 代码添加到您的控制器文件中 喜欢这里

          class Form extends Controller {
          
            function Form() {
              parent::Controller();   
            }
          
            function index() {    
              $this->load->view('form');
            }
          
          function search(){
          //your code here
          }
          }
          

          【讨论】:

            猜你喜欢
            • 2012-11-24
            • 2011-06-25
            • 1970-01-01
            • 2011-01-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-05
            相关资源
            最近更新 更多