Welcome Guest, Not a member yet? Register   Sign In
What view should I load if the validation fails?
#1

[eluser]tommo.wilson[/eluser]
Hi all,

This is probably a very simple question, but I'm brand new to CI and I'm getting stuck.

I want my website to have a new user registration form and also a login form. I want the login form to be displayed on every page.

Look at the code below. This is the controller that is called after clicking my login submit button. Keep in mind this could be submitted from any page. ie. home, registration, about us etc.

Code:
class Login extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('form_validation');
            
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
            
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('XXXXX');
        }
        else
        {
            $this->load->view('login_success');
        }
    }
}

My question is what do I put where it says XXXXXX? How do I know which view to load if the validation fails? I can't put something static in there like "registration" because if the user tries to login on the about us page and the login fails they will be redirected to the registration page. It would look weird and be confusing.

Thanks in advance guys
Tom
#2

[eluser]Cro_Crx[/eluser]
You should load the login form with errors on it if the user unsuccessfully tries to login. How is the user getting to the Login controller at the moment ? Are they posting from another page ?
#3

[eluser]tommo.wilson[/eluser]
But the login form is just that...a login form. It doesn't contain the rest of my template or any information in the content area.

This is from my template view:
I pass the template information on what content to put in the page. See how the login box is included on all of my pages. The only thing that changes is the page_content.

Code:
$this->load->view('head');
$this->load->view('login_box');
$this->load->view($page_content);
$this->load->view('foot');

The user could have got to my login controller from any of my pages. /registration or /about or /products etc.

From any of those pages the data is posted to my login controller.

I just don't know where to send them after it has been posted there because It might not be the original location.
#4

[eluser]WebsiteDuck[/eluser]
You can store $page_content in a hidden form input in the login_box so that you can send them back to the page they were on if login fails.

Code:
<input type="hidden" name="submitfrom" value="<?=$page_content?>" />

Then in your validation, send them to the template with page_content set to $this->input->post('submitfrom');
#5

[eluser]tommo.wilson[/eluser]
Ahhh, that works perfectly.

Thanks a lot.

Tom




Theme © iAndrew 2016 - Forum software by © MyBB