我下载了CodeIgniter 2.1.0,我在CodeIgniter 2.1.0上跟着tutorial。为什么我的网址重定向不起作用?
问题是,当我试图在这个URL提交表单:
http://localhost/CodeIgniter/index.php/news/create
页面被重定向到这个网址:
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');
}
}
我注意到在加载网页时当表单页面,下面的代码段将执行但是,其他部分从未得到要执行的更改。
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
,网址改为
http://localhost/CodeIgniter/index.php/news/localhost/CodeIgniter/index.php/news/create
和页面显示404 Pages Not Found
什么是您的表单的“行动”?我知道你使用'form_open()',但只是幽默我,并检查HTML输出。 – 2012-01-15 19:54:49
表单是为数据库插入新消息,其中包括新闻标题和新闻正文。 – qkzhu 2012-01-15 20:04:43
所有表单都有一个“action”属性,我在问HTML输出中的值是多少。 – 2012-01-15 20:41:20