Welcome Guest, Not a member yet? Register   Sign In
Load views after posting a form!
#1

[eluser]Unknown[/eluser]
Hello everyone!

I'm new to the codeigniter framework and I'm impressed of how fast you can write code with it! The only problem I have is the following and I hope there is a good way to solve it!

I will keep it simple so let's say I have 2 controllers (login.php & site.php) and 1 view (home.php).

site.php
Code:
<?php

class Site extends Controller {

    function index()
    {
        $data['a'] = 'example';
        $data['b'] = 'test';

        $this->load->view('home.php', $data);
    }

}

login.php
Code:
<?php

class Login extends Controller {

    function index()
    {
        // Run form validation and if ok do the following
        redirect('site');
        //else
        $data['a'] = 'example';
        $data['b'] = 'test';

        $this->load->view('site', $data);
    }

}

home.php
Code:
html code + form that posts to login.php

My problem is that because the form posts the data to the login.php controller, (if the validation fails and I want to show the errors) I have to load the view again and enter the array $data again because it is essential to the view.

In this case it's ok because this is a small array. But what if I have a view which requires a lot of data (and some of them is from a database!). I have to recreated the data every time I want to load a view from a different controller? Is there a way to load the view by calling the site/index and keep intact the form validation messages?

Thanks in advance.. I hope there is an easy solution to this!
#2

[eluser]richzilla[/eluser]
Ideally, you should put your validation functions in the same controller your view is being called from, then redirect after youve validated your form. However, if you want to access the same controller function from many controllers, you should look at the documentation for MY_Controller:

http://codeigniter.com/wiki/MY_Controlle...ontroller/

if your into your OO, You then extend all of your own controllers of this class. In practice what this means is that you can share the same function between multiple controllers. To save you having to reuse old code for loading things for your data array, you could check for the presence of a post array, That way you can simply reload the values from your post array, instead of going back to the database to get them.

EDIT:

I think this might better explaine what im getting at:
Code:
function edit_sub($user_id = 0)
    {
        //if there is a user in the URI or one has been provided by the form validation
        //load the form with the necessary user details
        if($this->uri->segment(4) || ($user_id > 0))
        {
            if($this->uri->segment(4))
            {
                $sub_id = $this->uri->segment(4);
            }
            else
            {
                $sub_id = $user_id;
            }
            $data['user'] = $this->sub_model->get_sub_user($sub_id);
                        $data['businesses'] = $this->sub_model->get_distinct_businesses();
            $this->load->view('global/header');
            $this->load->view('admin/edit_sub_user',$data);
            $this->load->view('global/footer');
        }
        else
        {
            $this->form_validation->set_error_delimiters('<div class="error">','</div>');

                        $this->form_validation->set_rules('cust_group','Customer Group','callback_valid_selection');
            $this->form_validation->set_rules('name','Name','required');
            $this->form_validation->set_rules('email','Email','valid_email');
            $this->form_validation->set_rules('telephone','Telephone','required|max_length[20]');
            $this->form_validation->set_rules('tvid','Teamviewer ID','max_length[12]');
            $this->form_validation->set_rules('tvpass','Teamviewer Password','max_length[20]');
            $this->form_validation->set_rules('address','Address','max_length[400]');
            $this->form_validation->set_rules('business','Business Name','max_length[255]');


            //if validation fails, load page , and send user id
            if($this->form_validation->run() == FALSE)
            {
                $this->edit_sub($this->input->post('user_id'));
            }
            else
            {

                $data = array(
                        'sub_user_name' => $this->input->post('name'),
                        'sub_user_tel' => $this->input->post('telephone'),
                        'user_email' => $this->input->post('email'),
                        'sub_user_tvid' => $this->input->post('tvid'),
                        'sub_user_cust_group' => $this->input->post('cust_group'),
                        'sub_user_tvid_pass' => $this->input->post('tvpass'),
                        'sub_user_address' => $this->input->post('address'),
                        'sub_user_business' => $this->input->post('business')
                );

                $this->sub_model->admin_add_sub_user($data,$this->input->post('user_id'));
                $this->session->set_flashdata('status','The new user was updated succesfully');
                                if($this->uri->segment(1) == 'admin')
                                {
                                    redirect('admin/customers');
                                }
                                else
                                {
                                    redirect('user/customers');
                                }
                
            }
        }
    }

So what you can see here, if it cant find a user id in either the uri or in the function arguments, it knows its supposed to try and validate the information. If the validation function fails, it calls the function again, this time with whatever your variable is as a function argument. If your function does not depend on a variable, you can simply call the function again, with a check for data in the post array.
#3

[eluser]Unknown[/eluser]
Thanks ricardino for your help!

I think that MY_Controller is perfect for my needs! I can put in there a function that loads all the data I need and just call it every time before loading a view.

The problem I was having was that I wanted to load the meta data of the views from the database (the admin can change them), so I had to load the data and passed it to the view each time Sad




Theme © iAndrew 2016 - Forum software by © MyBB