【发布时间】:2012-01-15 19:51:58
【问题描述】:
我下载了 CodeIgniter 2.1.0,并在 CodeIgniter 2.1.0 上关注了tutorial。
问题是当我尝试在此 URL 中提交表单时:
http://localhost/CodeIgniter/index.php/news/create
页面被重定向到这个 URL:
http://localhost/CodeIgniter/index.php/news/localhost/CodeIgniter/index.php/news/create
我厌倦了很多改变 route.php 的方法,但它不起作用。我该如何解决这个问题??
route.php
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
这是表单页面的代码:
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
我注意到当表单页面加载时,下面的代码段会执行,但是,else 段从来没有得到改变来执行。
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
这是表单页面,没什么特别的,调用如上所示的create函数即可。 create.php
<h2>Create a news item</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create') ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
这是表单页面的 HTML:
<html>
<head>
<title>Create a news item - CodeIgniter 2 Tutorial</title>
</head>
<body>
<h1>CodeIgniter 2 tutorial</h1><h2>Create a news item</h2>
<form action="localhost/CodeIgniter/index.php/news/create" method="post" accept-charset="utf-8">
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form><strong>©2012</strong>
</body>
</html>
当我按下create new item 按钮时,URL 变为
http://localhost/CodeIgniter/index.php/news/localhost/CodeIgniter/index.php/news/create
页面显示404 Pages Not Found
【问题讨论】:
-
您的表单的
action是什么?我知道你在使用form_open(),但请幽默并检查 HTML 输出。 -
表格是向数据库中插入一条新的新闻,包括新闻标题和新闻正文。
-
所有表单都有一个
action属性,我在问你的HTML输出中那个值是什么。 -
OK,表单页面将由上面的create()函数生成。如果表单验证器运行没有错误,它会将页面重定向到success.php,它只会告诉你已经提交了一个表单;否则它将重新加载 for 页面。
-
请在您查看表单时将浏览器中的 HTML 提供给我们
标签: php codeigniter codeigniter-2 codeigniter-url