Php – codeigniter displaying form_open

codeigniterformsPHP

I am trying to learn codeigniter and for some reason I can't do the simplest of things.

Basically I am creating a form on a login page. But I am getting

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

and if I comment out the validation_errors line, I get

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

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

<?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\

<?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/

<?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['default_controller'] = "pages/view";
$route['(:any)']="pages/view/$1";
$route['404_override'] = '';

this is what I think happens and if I am 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.

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 don't 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 someone explain the flow of this, please?

Best Answer

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

Related Topic