CodeIgniter Forums
Handling form inside header template file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Handling form inside header template file (/showthread.php?tid=68208)



Handling form inside header template file - CreativeMess - 06-09-2017

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?


RE: Handling form inside header template file - natanfelles - 06-10-2017

(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.