1

我开始在一个工作项目中使用 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))。如何使其转到特定功能来处理表单提交?

4

2 回答 2

0
form_open('submit', $attributes) 

must be

form_open('/path_to_my_page', $attributes) 

if I understand you correctly ...

form_open('/view/page', $attributes) 
于 2012-10-26T10:12:44.090 回答
0

我想通了!感谢 Svetlio 以正确的方式指出我。我是这样做的:

1)在表单初始化中正确设置路径:

echo form_open('contactos/submit', $attributes); 

2)进入路由配置(application/config/ routes.php),添加路由异常到这个文件

$route['contactos/submit'] = "pages/submit";

它的作用是,当程序被要求输入路径“contactos/submit”时,它指向pages.php控制器中的函数提交(这就是我正在寻找的......)

3)在您的控制器中定义函数提交,以正确处理表单的提交

public function submit()
{
/* handle the form code */
    }
于 2012-10-26T19:56:39.240 回答