我开始在一个工作项目中使用Codeigniter,我仍然掌握它......
绕过Codeigniter教程,我开始构建只有一个控制器来处理视图的网站,但现在我无法处理该控制器上的表单提交。我在这里失去了理智......:P
这是控制器的唯一功能(pages.php)
public function view($page = 'home')
{
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
// handle ACTIVE class
$current_page = uri_string();
$this->Mp->isactiveset($this->session->userdata('active'),$current_page);
$this->Mp->newactive($this->session->userdata('active'),$current_page);
// testing database query and passing variables
$passar = $this->Mp->testedb();
// get ap types
$aptype = $this->Mp->getaptype();
// passing data to view
$data['estado'] = $passar; // Testing db connection
$data['title'] = ucfirst($page); // Homepage page
$data['aptypes'] = $aptype; // Ap types
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
我有处理表格准备的功能
public function index()
{
$this->form_validation->set_rules('nome', 'Username', 'required|min_length[3]|max_length[25]|');
$this->form_validation->set_rules('telemovel', 'Telephone', 'numeric|exact_length[9]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('mensagem', 'MessageBody', 'required|min_length[10]');
if ($this->form_validation->run() == FALSE)
{
// handle ACTIVE class
$current_page = uri_string();
$this->Mp->isactiveset($this->session->userdata('active'),$current_page);
$this->Mp->newactive($this->session->userdata('active'),$current_page);
// get ap types
$aptype = $this->Mp->getaptype();
// testing database query and passing variables
$passar = $this->Mp->testedb();
// passing data to view
$data['estado'] = $passar; // Testing db connection
$data['aptypes'] = $aptype; // Ap types
$this->load->view('templates/header', $data);
$this->load->view('pages/home', $data);
$this->load->view('templates/footer', $data);
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('pages/submit', $data);
$this->load->view('templates/footer', $data);
}
}
这就是route.php的样子(在第一个答案之后,我现在知道这是相关的)
$route['default_controller'] = "pages/view";
$route['(:any)'] = 'pages/view/$1';
$route['(\w{2})/(.*)'] = '$2';
$route['(\w{2})'] = $route['default_controller'];
$route['404_override'] = '';
在视图中,当我执行form_open('submit',$ attributes)时,除了视图之外,它不会进入我的控制器上的任何函数。显然我已经在pages.php上放置了表单处理函数,重新命名了它,尝试了不同的表单提交页面(form_open('我试过的页面',$ attributes))。如何使其转到特定功能来处理表单提交?
答案 0 :(得分:0)
form_open('submit', $attributes)
必须是
form_open('/path_to_my_page', $attributes)
如果我理解正确的话......
form_open('/view/page', $attributes)
答案 1 :(得分:0)
我已经明白了!感谢Svetlio以正确的方式指点我。我是这样做的:
1)在表单初始化中正确设置路径:
echo form_open('contactos/submit', $attributes);
2)转到路由配置(application / config / routes.php )并将路由例外添加到此文件
$route['contactos/submit'] = "pages/submit";
这样做的是,当程序被要求提供“contactos / submit”路径时,它指向 pages.php 控制器中的提交功能(是我在寻找......)
3)在你的控制器中定义函数submit,以正确处理表单的提交
public function submit()
{
/* handle the form code */
}