gpt4 book ai didi

php - 如何使用 CodeIgniter 处理表单

转载 作者:IT王子 更新时间:2023-10-29 00:07:53 25 4
gpt4 key购买 nike

我是 CodeIgniter 的新手。我需要处理一个表格。我在 View 中有一个 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>

和表单 Controller

class Form extends Controller {

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

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

}

我有一个 View 文件 search.php 但是当它被处理时它显示找不到页面...

最佳答案

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

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

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

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

换句话说, View 决定了数据将如何显示。 Controller 将请求映射到 View 。

当您希望在 View 中显示复杂和/或不断变化的数据时,情况会稍微复杂一些。为了维护 separation of concerns MVC 需要 CodeIgniter 还为您提供 Models .

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

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

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

Request - Response
(来源: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);
}

关于php - 如何使用 CodeIgniter 处理表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2279282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com