Welcome Guest, Not a member yet? Register   Sign In
Handling form inside header template file
#1

Hey, I'm new to CodeIgniter and loving the framework so far however have a coding question that I've been stumped on today.

I'm looking to have a login form in the views/templates/header.php file which is included on all the pages of the website. I am writing the validation and have the following function inside a User controller,

Code:
public function login(){
   $this->form_validation->set_rules( 'username', 'Username', 'required' );
   $this->form_validation->set_rules( 'password', 'Password', 'required' );

   if($this->form_validation->run() === false){

   } else {
       ......
   }
}

However if a user fails the validation I would like them to reload the page they were pervious on and show the validation errors. Since the form is pointing to /users/login is there a way to do this? Would I just find out what the last page they were on and redirect page and include
Code:
<?php echo validation_errors(); ?>
inside the header.php file? or would it be more complex than this? If so, how could I redirect back to the previous page?
Reply
#2

(This post was last modified: 06-10-2017, 09:17 AM by natanfelles. Edit Reason: . )

(06-09-2017, 03:29 PM)CreativeMess Wrote: Hey, I'm new to CodeIgniter and loving the framework so far however have a coding question that I've been stumped on today.

I'm looking to have a login form in the views/templates/header.php file which is included on all the pages of the website. I am writing the validation and have the following function inside a User controller,

Code:
public function login(){
   $this->form_validation->set_rules( 'username', 'Username', 'required' );
   $this->form_validation->set_rules( 'password', 'Password', 'required' );

   if($this->form_validation->run() === false){

   } else {
       ......
   }
}

However if a user fails the validation I would like them to reload the page they were pervious on and show the validation errors. Since the form is pointing to /users/login is there a way to do this? Would I just find out what the last page they were on and redirect page and include
Code:
<?php echo validation_errors(); ?>
inside the header.php file? or would it be more complex than this? If so, how could I redirect back to the previous page?


If do you want to redirect back after form validation errors, you can save the errors in a session as Flashdata and show it in the previous page.

Also you can use some javascript validation library in the frontend as bootstrap-validator to use in the client side. After you can send the post data with Ajax and validate the errors in the server side and show it in the same page. This way you will not have to go and back from the main login page.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB