2010-02-17 112 views
13

我是CodeIgniter的新手。我需要处理一个表单。我在视图如何使用CodeIgniter处理表单

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

} 

有form.html页,我有一个视图文件的search.php但是当它被处理,它显示找不到网页...

回答

10

如果没有控制器加载并显示它,则查看文件是无用的。您必须创建一个控制器来接收表单数据,处理它,然后显示处理结果。

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

<?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的许多成员将帮助你。

0

尝试在您的操作中使用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> 

指数只有在你的控制器使用。所以在我的例子中上面,你会想是这样的:

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

function process_search() 
{ 

    print "<pre>"; 

    print_r($_POST); 

    print "</pre>"; 
} 
31

中号 .odel V .iew Ç像笨.ontroller设置中的视图是用户界面元素。他们不应该解析结果。

如果我没有记错,你在找什么做的是通过数据从www.yoursite.com/index.php/formwww.yoursite.com/index.php/search

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

CI中(和,据我了解,在任何MVC系统,无论何种语言的)你ControllerForm调用它加载form.htmlView到自身一个函数,然后运行它。 View生成用户与之交互的代码(通常是HTML,但不一定是)。当用户发出View无法处理的请求(请求更多数据或其他页面)时,它会将该请求传回给Controller,Controller将加载更多数据或另一个View。

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

当您希望在视图中显示复杂和/或更改的数据时,它会变得稍微复杂一些。为了保持MVC要求的CodeIgniter也为您提供Models

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

  • 确保数据格式正确。
  • 确保数据中没有任何内容(恶意或其他)可能会破坏它的目标环境。
  • 具备提供Ç .reating,ř .eading,ü .pdating,和d在上述约束内.eleting数据的方法。

Akelos具有良好的图形布局MVC的组件:

Request - Response http://svn.akelos.org/trunk/docs/images/akelos_mvc.png

话虽这么说,最简单的(读“最简单”的,不是“最可扩展”)的方式来完成你想要做的是:

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); 
} 
+3

+1 - 我的乐观Upvote。这很清楚,很有吸引力。我没有足够的技巧来评估它的真实性,但我会看看我是否可以使用它来变得如此。 – Smandoli 2010-08-06 17:43:04

+0

'$ term = $ this-> input-> post('search');'here ...'search'是表单的名称,对吗? – 2012-11-17 15:44:39

+2

@b_dubb - 'search'是*字段的名称*:''。 – 2012-11-19 17:41:49

0

<?php echo form_open('form/search');?> 和autoload.php文件替换该<form action="search">添加$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 
} 
} 
1

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

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>