如何使用CodeIgniter处理表单 [英] How to process a form with CodeIgniter

查看:95
本文介绍了如何使用CodeIgniter处理表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CodeIgniter的新人。我需要处理一个表单。我有一个form.html页面查看

I am new to CodeIgniter. I need to process a form. I have a form.html page in view

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

}

.php,但在处理时显示未找到...

and I have an view file search.php but when it is processed it shows page not found...

推荐答案

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

In M.odel V.iew C.ontroller setups like CodeIgniter the Views are user interface elements. They should not be parsing results.

如果我没有错,你要做的是传递数据从 www.yoursite.com/ index.php / form www.yoursite.com/index.php/search

If I am not mistaken, what you are looking to do is pass data from www.yoursite.com/index.php/form to www.yoursite.com/index.php/search

在非结构化php中,您可能有一个 search.php form.html / code> 。用户将导航到 yoursite.com/form.html ,这将调用 yoursite.com/search.php ,这可能会重定向到 yoursite.com/results.php

In unstructured php you might have a form.html with a form action of search.php. A user would navigate to yoursite.com/form.html, which would call yoursite.com/search.php, which might redirect to yoursite.com/results.php.

在CodeIgniter在任何MVC系统中,无论使用何种语言)您的控制器 Form 调用加载 form.html 查看 进入,然后运行它。 视图会生成用户与之互动的代码(通常为HTML,但不一定是) 。当用户发出View不能处理的请求(请求更多数据或另一个页面)时,它将该请求传回到控制器,Controller将加载更多数据或另一个View。

In CodeIgniter (and, as far as I understand it, in any MVC system, regardless of language) your Controller, Form calls a function which loads the form.html View into itself and then runs it. The View generates the code (generally HTML, but not necessarily) which the user interacts with. When the user makes a request that the View cannot handle (requests for more data or another page) it passes that request back to the Controller, which loads in more data or another View.

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

In other words, the View determines how the data is going to be displayed. The Controller maps requests to Views.

当您想要在视图中显示复杂和/或变化的数据时,它会变得稍微复杂一些。为了维护MVC还需要CodeIgniter的 关注问题的分离 为您提供模型

It gets slightly more complicated when you want to have complex and / or changing data displayed in a view. In order to maintain the separation of concerns that MVC requires CodeIgniter also provides you with Models.

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

Models are responsible for the most difficult part of any web application - managing data flow. They contain methods to read data, write data, and most importantly, methods for ensuring data integrity. In other words Models should:


  • 确保数据格式正确。

  • 确保

  • 拥有 C .reating, R 的方法, /strong>.eading, U .pdating和 D 。在上述约束内删除数据。

  • Ensure that the data is in the correct format.
  • Ensure that the data contains nothing (malicious or otherwise) that could break the environment it is destined for.
  • Possess methods for C.reating, R.eading, U.pdating, and D.eleting data within the above constraints.

Akelos 有一个很好的图形,展示了MVC的组件:

Akelos has a good graphic laying out the components of MVC:

话虽这么说,最简单的(读最简单,而不是最大可扩展)方法来完成你想做的是:

That being said, the simplest (read "easiest", not "most expandable") way to accomplish what you want to do is:

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

这篇关于如何使用CodeIgniter处理表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆