简体   繁体   中英

Codeigniter: How to use form_open() in multiple view files

I am trying to get users to fill in much information so I need more than one form and page. I made a main_view.php which has a side bar on the left with links to sub1.php , sub2.php , sub3.php . On the right half of main_view.php , it displays the sub pages with corresponding forms. Part of the main_view.php looks like this:

<?php $view_path = "../../application/views/"?>

<span id="theFormChanger" > 
<?php               
?>
</span>

var currentPage = 0; 
var subviews = ['sub1.php', 'sub2.php','sub3.php'];

$('#sub1').click(function(){ 
           currentPage = 1;
           $('#theFormChanger').load(viewpath + subviews[currentPage]);
});

Part of the code of sub view pages:

<?php echo form_open('v_controller'); ?>
<?php echo form_input(array( 'type' => 'text', 'id' => 'demail', 'name' =>'demail')); ?>
<?php echo form_input(array( 'type' => 'text', 'id' => 'dname', 'name' => 'dname')); ?>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?>

For ../application/controllers/ ,there is a v_controller.php :

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

public function index()
{
    $this->load->helper('form');
    $this->load->view('sub1');
    $data = array(
        'User_Name' => $this->input->post('dname'),
        'User_Email' => $this->input->post('demail'));
}?>

Every time when I go to localhost:8000/main/main_view , the left part is fine but the right part says "Fatal error: Call to undefined function form_open() in main_view.php"

I searched around but couldn't find answers. I made sure everything is loaded in autoload.php .

Is this a routing problem? I can't directly go to view files? Please help me. Thank you!

You can load views on to a view file like so

application > views > default.php

<?php $this->load->view('template/common/header');?>
<?php $this->load->view('template/common/navbar');?>
<?php $this->load->view('template/' . $page);?>
<?php $this->load->view('template/common/footer');?>

And then on controller

<?php

class Example extends CI_Controller {

    public function index() {
        $data['page'] = 'common/example';
        $this->load->view('default', $data);
    }
}

you need to create NameOfController/NameOfMethod in the form open method in your view page.

replace our code by this one , it will be work for you.

<?php echo form_open('v_controller/index'); ?>

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