php codeigniter 显示 form_open

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10980116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:31:09  来源:igfitidea点击:

codeigniter displaying form_open

phpformscodeigniter

提问by Ahmed-Anas

hey guys was hoping you could help me out.

嘿伙计们希望你能帮助我。

im trying to learn codeigniter and for some reason i cant do the simplest of things.

我正在尝试学习 codeigniter,但由于某种原因,我无法做最简单的事情。

basically im creating a form on a login page. but im getting

基本上我在登录页面上创建一个表单。但我得到

Fatal error: Call to undefined function validation_errors() in A:\work\codeigniter\ci\application\views\pages\login.php on line 5

致命错误:在第 5 行调用 A:\work\codeigniter\ci\application\views\pages\login.php 中未定义的函数 validation_errors()

and if i comment out the validation_errors line i get

如果我注释掉 validation_errors 行,我会得到

Fatal error: Call to undefined function form_open() in A:\work\codeigniter\ci\application\views\pages\login.php on line 5 ...

致命错误:在第 5 行调用 A:\work\codeigniter\ci\application\views\pages\login.php 中未定义的函数 form_open() ...

Error message

错误信息

( ! ) Fatal error: Call to undefined function validation_errors() in A:\work\codeigniter\ci\application\views\pages\login.php on line 3
Call Stack
#   Time    Memory  Function    Location
1   0.0004  697328  {main}( )   ..\index.php:0
2   0.0010  790176  require_once( 'A:\work\codeigniter\ci\system\core\CodeIgniter.php' )    ..\index.php:202
3   0.0135  2313080 call_user_func_array ( )    ..\CodeIgniter.php:359
4   0.0135  2313160 Pages->view( )  ..\CodeIgniter.php:359
5   0.0135  2313288 CI_Loader->view( )  ..\pages.php:9
6   0.0135  2314232 CI_Loader->_ci_load( )  ..\Loader.php:419
7   0.0138  2363392 include( 'A:\work\codeigniter\ci\application\views\pages\login.php' )   ..\Loader.php:833

this is my code.

这是我的代码。

login.php located at view\pages

login.php 位于 view\pages

<?php echo validation_errors(); ?>

<?php echo form_open('user/login') ?>

    <label for="Id"> User Id</label>
    <input type="input" name="Id" /> <br/>

    <label for="Password">User Password</label>
    <input type="input" name="Password" />
    <br/>

    <input type="submit" name="submit" value="log in" />

</form>

user.php located at controllers\

user.php 位于控制器\

<?php
    class User extends CI_controller{
    public function login()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');

        //i have a file views/pages/main.php which just says "this is main"
        $this->load->view('pages/main'); 
    }
    }

pages.php located at controllers/

pages.php 位于控制器/

<?php

    class Pages extends CI_Controller{
    public function view($page){
        if(!isset($page)||!file_exists("application/views/pages/$page.php")){
        show_404();
        }

        $this->load->view("pages/$page");


    }
    }

routes.php located at config/

route.php 位于 config/

$route['default_controller'] = "pages/view";
$route['(:any)']="pages/view/";
$route['404_override'] = '';

this is what i think happens and if im wrong please correct me. form_open('user/login') makes the "action" of the html form element point to a method "login" of class "user" located in controlers.

这就是我认为发生的事情,如果我错了,请纠正我。form_open('user/login') 使 html 表单元素的“action”指向位于控制器中的类“user”的方法“login”。

also, i googled a lot and pretty much everyone else that was getting this error was getting it because they hadnt done

另外,我在谷歌上搜索了很多,几乎所有其他收到此错误的人都收到了,因为他们还没有完成

$this->load->helper('form');
$this->load->library('form_validation');

also, i dont get the string in form_open points to the location for the "action" attribute in the html form element, why is it neccessary to load form helper and form validation library there? can some explain the flow of this please.. would be really appreciated..thanks..

另外,我没有得到 form_open 中的字符串指向 html 表单元素中“action”属性的位置,为什么需要在那里加载表单助手和表单验证库?可以请解释一下这个流程吗..真的很感激..谢谢..

回答by Ahmed-Anas

basically the problem was that the controller that was calling the view with the form should contain

基本上问题是使用表单调用视图的控制器应该包含

$this->load->helper('form');
$this->load->library('form_validation');

and not the controller that is being called by the form action

而不是表单操作调用的控制器

回答by scaryguy

Try loading form helper at your autoload.phpfile inside configfolder.

尝试在autoload.php文件config夹内的文件中加载表单助手。

This would be a good practise:

这将是一个很好的做法:

$autoload['helper'] = array('url','text','form');