I have 2 methods inside my controller, one is responsible for generating the signup page and another is responsible for handing the signup form submission.
<?php
class Signup_c extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
$data['title'] = 'Sign Up';
$data['months'] = array(
'1' => 'January',
'2' => 'February',
'3' => 'March',
'4' => 'April',
'5' => 'May',
'6' => 'June',
'7' => 'July',
'8' => 'August',
'9' => 'September',
'10' => 'October',
'11' => 'November',
'12' => 'December'
);
$this->load->view('signup_v', $data);
}
function submit() {
// validation rules here...
// validate
if ($this->form_validation->run() === FALSE) {
$this->load->view('www/signup_v');
}
else {
// add info to database here...
$this->load->view('www/signup_success_v');
}
}
}
Now the problem is this, if there is a validation error then the user is returned back to the signup page and the validation errors are displayed. However no title or date is shown because those variables were defined inside the index() method and not the submit() method.
What is the best solution to this, I don’t want to repeat my self and copy over those 2 variable declarations inside the signup method too. Is there a way to make it work in both method’s views?
Why not assign class variables? before construct:
then inside your methods access them with $this->title and $this->months
Additionally, why not just post back to your index method and have it do post processing before anything else? so your index method begins with something like this: