0

I am trying to create a master & view page layout, with Header, left menu, right content view & a footer. Each content page is written in <div>s in separate pages.

With respect to my left menu link clicks, the corresponding page is loaded in content view. I used jQuery for this purpose, but when I use form_open() in content view <div>, it returns:

undefined function form_open()

I have tried both

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

and

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

But neither are working.

When I use form_open() in a view page and load it via $this->load->view() in controller, I'm able to use the form_open method

Here is controller class, template.php & content.php files

class Home extends CI_Controller {

public function index(){
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->view();        
}

public function view() {
    $this->load->view('templates/template');

    .
    .
    .
}

application/views/templates/template.php

.
.
.
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
    $('#leftmenu_main ul li a').click(function(){
        $('#content_main').load('<?=base_url()?>application/views/templates/content.php #' + $(this).attr('href'));
        return false;
    });
}



<div id="leftmenu">
<div id="leftmenu_main">                       
    <ul>
        <li><a href="createnewuser">Create New User</a></li>
        <li><a href="createnewtask">Create New Task</a></li>
    </ul>
</div>
</div>

<div id="content_main">
    .
    .
    .
</div>

application/views/templates/content.php

<div id="createnewuser">    
<?=form_open(controller/method);?>
    .
    .
    .
<?=form_close();?>
</div>
<div id="createnewtask">    
<?=form_open(controller/method);?>
    .
    .
    .
<?=form_close();?>
</div>

When i use view() of Home Controller like the following, & place one of my div's "content" as a page in views/pages/ , it works & 'm able to use the form_open() tag ...

public function view() {
    $page = 'createnewtask';       
    $this->load->view('templates/header'); 
    $this->load->view('pages/'.$page);        
    $this->load->view('templates/footer');
}

I have searched a lot for this but unable to find a solution.

Is there something which I need to load / configure for using form_open() in a separate page <div>?

4
  • give your controller and view files Commented Jun 4, 2013 at 13:38
  • 3
    How are you loading these "separate" pages? You need to have a controller load the views for them to know that form_open is. You say you are using jQuery. Are you using AJAX? Are you having the AJAX load the view file directly? You can't do that. You need to have the AJAX load the controller and have the controller load the view with $this->load->view. Commented Jun 4, 2013 at 14:18
  • @RocketHazmat "m not using AJAX, i use home contoller which loads form & other form_validation helpers and then using jquery changes the #content_main <div>. i have updated post with contoller & template file details .. Commented Jun 5, 2013 at 5:47
  • @RahulArackal: You are using AJAX. $('#content_main').load is AJAX. Commented Jun 5, 2013 at 13:07

1 Answer 1

2

You are using AJAX to load the view file.

$('#content_main').load('<?=base_url()?>application/views/templates/content.php #' + $(this).attr('href'));

This is ajax. Problem is, you are loading the view file directly. Do not do this! That is why form_open isn't working. The view file doesn't know it's there.

You need to make a controller function that calls $this->load->view('templates/content'); and set the AJAX call to that.

class Home extends CI_Controller {

    public function index(){
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->view();        
    }

    public function view() {
        $this->load->view('templates/template');

        .
        .
        .
    }

    public function content(){
        $this->load->view('templates/content');
    }
}

Then change the AJAX call to:

$('#content_main').load('<?=base_url()?>home/content #' + $(this).attr('href'));

Now you are loading the view via the controller, which loaded the form helper, so form_open will work.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man, got it working!!!, there is a .load() method in jQuery na, so i thought it is the jQuery method,I got the jQuery.load() snippet from some tutorial. 'm jst starting to learn jQuery, sorry for the wrong info, ... :) ::Vote UP requires 15 reputation!!! :(
Here's the docs for .load: api.jquery.com/load It's just a shorthand AJAX method :-D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.