简体   繁体   中英

CodeIgniter PHP Fatal error: Call to undefined function form_open()

I am quite new to PHP and CodeIgniter itself. I know that this error will be encountered when you don't load the form helper. However, I have added in and still face this error.

Please take a look at my codings.

This is my View: Create

<?php echo form_open('studCred/Create'); ?>
                 <?php echo validation_errors(); ?>
                    <div class="row">
                      <div class="col-xs-6 col-sm-6 col-md-6">
                        <div class="form-group">
                          <label>Username</label>   
                          <input type="text" class="form-control" name="username" placeholder="Username">

                        </div>
                      </div>
                      <div class="col-xs-6 col-sm-6 col-md-6">
                        <div class="form-group">
                         <label>Email</label>
                        <input type="email" class="form-control" name="email" placeholder="Email">
                    </div>
                      </div>
                      </div>
                       <div class="row">
                      <div class="col-xs-6 col-sm-6 col-md-6">
                      <div class="form-group">
                    <label>Password</label>
                    <input type="password" class="form-control" name="password" placeholder="Password">
                    </div>
                    </div>

                      <div class="col-xs-6 col-sm-6 col-md-6">
                        <div class="form-group">
                          <label>Admin No</label>
                              <input type="text" class="form-control" name="adminNo" placeholder="AdminNo">

                        </div>
                      </div>
                      <div class="col-xs-6 col-sm-6 col-md-6">
                        <div class="form-group">
                          <label>Phone number</label>
                           <input type="number" class="form-control" name="phone" placeholder="Phone">

                        </div>
                      </div>
                       </div>

                    <input type="submit" value="Submit" name="save" class="btn btn-skin btn-block btn-lg">

                    <p class="lead-footer">* We'll contact you by phone & email later</p>


                  </form>

This is my Controller: studCred

    class studCred extends CI_Controller {

     public function __construct()
{
    parent::__construct();

    $this->load->library('form_validation');    
     $this->load->helper('form');
     this->load->model('studCredModeller');
}
    //Register students
    public function create() {

    //load registration view form
    $this->load->view('studCred/Create')

    ;

    //set validation rules
        $this->form_validation->set_rules('username', 'Username', 'required|callback_check_username_exists');
        $this->form_validation->set_rules('adminNo', 'AdminNo', 'required|callback_check_adminNo_exists');
        $this->form_validation->set_rules('email', 'Email', 'required|callback_check_email_exists');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('phone', 'Phone',  'required');

         if ($this->form_validation->run() === FALSE){
             $this->load->view('studCred/Create');
         }
         else 
         {
             this->studCredModeller->saveRecords();
             this->load->view('nypportal');


        }
    }
}

Model: studCredModeller

if (!defined('BASEPATH'))
    exit ('No direct script access allowed!');

class studCredModeller extends CI_Model
{
    public function saveRecords();
    {
         $this->load->helper('url');

        $data = array(
        'username' =>$this->input->post('username'),
        'admin_no' =>$this->input->post('adminNo'),
        'email' =>$this->input->post('email'),
        'password' => $this->input->post('password'),
        'phone' => $this->input->post('phone'));

        return $this->db->insert('stud_login', $data);

}
}

Thank you. Putting it into the config/autoload.php didn't work either.

Hope this will help you :

You have series of typo error in your code, so remove them one by one and then check again

create method should be Create , add $ to here this->studCredModeller->saveRecords(); and here this->load->view('nypportal');

You are also missing url helper in controller so use url helper in controller instead of using in model

Note : controller name should start with capital letter and must match with file name, and better use url helper and form helper in autoload.php

Your controller __construct() method should be like this :

public function __construct()
{
    parent::__construct();

    $this->load->library('form_validation');    
    $this->load->helper('form');
    $this->load->helper('url');
    this->load->model('studCredModeller');
}

And Remove ; after the method saveRecords(); in your model , should be like this:

public function saveRecords()
{
    $this->load->helper('url');
    $data = array(
      'name' =>$this->input->post('username'),
      'admin_no' =>$this->input->post('adminNo'),
      'email' =>$this->input->post('email'),
      'password' => $this->input->post('password'),
      'phone' => $this->input->post('phone')
    );
    return $this->db->insert('stud_login', $data);
}

for more : https://www.codeigniter.com/user_guide/general/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM