I have a template design for each page and it is working without any issues, now i have a problem with form validation and template.
my_controller //inside my controller
function __construct()
{
parent::__construct();
$this->load->library('template');
}
public function page1(){
$this->template->write('title', 'COMPANY');
$this->template->write_view('content', 'company');
$this->template->render();
}
public function validation(){
//for page1 form validation
//form validation here
if ($this->form_validation->run() == FALSE){
// here i need to call my page1 again to update the form error
// option1: $this->load->view('page1'); // this is just loading page1, not loading with my template, template file has header, footer, js and css file includes
// option2: $this->template->load('template', 'page1'); // it is loading page withing page
// option3: $this->template->write_view('content', 'page1');
// $this->template->render(); // this is same as option2
} else {
$this->load->view('formsuccess');
}
}
So my question is how do i can call same view (within one controller) for multiple time when it is already loaded with template?
EDITED for more clarity
My template file is just single file ( not separated like header, footer) will have all header information and footer information with common style sheet and script files.
I am having
<?php $content ?>
variable name inside the template to pass my content from controller.
for example see my page1 function(),
$this->template->write('title', 'COMPANY'); // to write a value on variable
$this->template->write_view('content', 'page1'); //view page to write on template
$this->template->render(); // final template render
This page is having form validation, if my form validation is FALSE, how do i can reload the template page with validation error?
I have tried so many way, please check my option1, 2, 3, i never got a solution to solve this, now please tell me how do i can solve this?
If I understand correctly, you want to display the validation errors?
Inside your View file, put the following:
Short IF-Statement Form:
<?php if ( validation_errors() ) :?>
<div>
<?php echo $validation_errors();?>
</div>
<?php endif;?>
Regular If-Statement Form:
<?php if ( validation_errors() ) {?>
<div>
<?php echo $validation_errors();?>
</div>
<?php }?>
Validation Errors Doc
Related
I had few forms in my project, they were submitted to public function's like site.com/email => site.com/validate_email, but then I realized that that's not what I want.
Now I need to make them submit to themselfs ,check and display validation errors.
What is the appropriate way to do this? Check for emptyness of $_POST and then call my new _validate_email(//that will return true or false) if post isn't empty?
Or something else not that noobish?:)
for example:
public function login()
{
$this->load->view('login');
}
public function login_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|trim|xss_clean|callback_validate_credentials');
$this->form_validation->set_rules('password','Password','required|md5');
if($this->form_validation->run())
{
//some stuff here
}
else
{
$this->load->view('login'); //or redirect()?
}
view:
<?php $this->load->view('header'); ?>
<div class="form-container">
<?=$this->form_validation->validation_errors();?>
<?php
$form_atr = array(
'id' => 'form-set'
);
echo form_open('main/login_validation', $form_atr);//this should be 'main/login'
?>
<div class="header">
/*
here goes other parts of form
*/
</div><!--END form-container -->`
<?php $this->load->view('footer'); ?>
So, basicaly i need to combine login() and login_validation(), but make it so that when user`s input incorrect i get reloaded page of the same view with the same URL and get validation errors displayed.
I've tried to put code of validation into the same function that displays form, but I can't figure out how to redirect or reload the view to show val.errors if any.
So, I think this way is correct(It really should be):
I made my login_validation() private by adding '_' before it, like so _login_validation()
Than I added an if() statement that contains $_POST form variables and i am cheking them with php isset() function, that way the code can determine when user submitted a form. And after all that I just call _login_validation() if inputs are set or load again my login view if not.
public function login()
{
if(isset($_POST['password']) && isset($_POST['email']))
{
$this->_login_validation();
}
else
{
$this->load->view('login');
}
}
and dont forget to process your form so it would submit to the same URL:
echo form_open('', $form_atr);
Hope that will help someone someday.
First of all, validation_errors() is one kind of flash data, so whenever you redirect the page with error the validation errors will be displayed, if you reload it second time it will not be displayed.
From your question i could not understand, do you want to show the errors or not.
if you want to show the errors:
then just redirect the page to login and in the login view add a alert div.
if you don't want to show the errors:
then just remove the alert div.
*i don't see any alert div in your login view, if validation errors are still displayed then may be alert div is in the header view
I have a bootstrap form where after filling data its successfully get inserted to database .Now i want to show detail view of form with filled data but it taking me back to create view after submit with data added to database. i guess i have problem with site url. for better understanding hereby i am attaching my code.
my create view file code is :
<form class="form-horizontal" id="job" action="<?php echo site_url('admission/add_students')?>" method="POST" name="job">
where as controllers(admission):
function add_students() {
$this->load->model('admission_detail_model');
$data=array(
'student_id'=>'La-0002'.$this->input->post('student_id'),
'father_name'=>$this->input->post('father_name'),
'mother_name'=>$this->input->post('mother_name'),
'fname'=>$this->input->post('first_name'),
'lname'=>$this->input->post('Last_name'),
'place_of_birth'=>$this->input->post('place_birth'),
'mother_tounge'=>$this->input->post('mother_tounge'),
'd_o_b'=>$this->input->post('DOB'),
'nationality'=>$this->input->post('nationality'),
'religion'=>$this->input->post('religion'),
'sc_st_obc'=>$this->input->post('sc_st_obc'),
'caste'=>$this->input->post('caste'),
'address'=>$this->input->post('address'),
'admitting_student'=>$this->input->post('Admit_std'),
'father_edu_qual'=>$this->input->post('father_q'),
'mother_edu_qual'=>$this->input->post('mother_q'),
'annual_income'=>$this->input->post('annual_income'),
'father_occupation'=>$this->input->post('father_occupation')
);
$this->admission_detail_model->add_students($data);
$this->index();
}
Model:
class Admission_detail_model extends CI_Model {
function add_students($data) {
$this->db->insert('students',$data);
return; }
Everything working fine i just want to add detail view after submit form not another create form View. For detail view i have controller defined in my base controller(admission) :
public function detailed_admission()
{
$this->load->helper('url');
$this->load->view('Header');
$this->load->view('side_menu');
$this->load->view('admission/detailted_view');
$this->load->view('footer');
}
when i try to replace site url in create view file
"<?php echo site_url('admission/detailted_view')?>"`
it does not enter data in database redirect directly to this view without any data.
This is my first question so if i had made any mistake please avoid it.
Thankyou for helping
Check my comments in the code.
function add_students() {
$this->load->model('admission_detail_model');
$data=array(
'student_id'=>'La-0002'.$this->input->post('student_id'),
'father_name'=>$this->input->post('father_name'),
'mother_name'=>$this->input->post('mother_name'),
'fname'=>$this->input->post('first_name'),
'lname'=>$this->input->post('Last_name'),
'place_of_birth'=>$this->input->post('place_birth'),
'mother_tounge'=>$this->input->post('mother_tounge'),
'd_o_b'=>$this->input->post('DOB'),
'nationality'=>$this->input->post('nationality'),
'religion'=>$this->input->post('religion'),
'sc_st_obc'=>$this->input->post('sc_st_obc'),
'caste'=>$this->input->post('caste'),
'address'=>$this->input->post('address'),
'admitting_student'=>$this->input->post('Admit_std'),
'father_edu_qual'=>$this->input->post('father_q'),
'mother_edu_qual'=>$this->input->post('mother_q'),
'annual_income'=>$this->input->post('annual_income'),
'father_occupation'=>$this->input->post('father_occupation')
);
//To insert the record in the database
$this->admission_detail_model->add_students($data); after
//open the detail view page
// $this->index(); why did you call index function ??
$this->detailed_admission(); //call detailed function just after the form submission or you can perform redirect('admission/detailed_admission')
}
I have some experience with php, but I just recently started learning Codeigniter. I used to have a website with a fixed navigation pane and sidebar, but the main section of the site loaded dynamically based on a Get variable. It was basically
include head.php
include navbar.php
include sidebar.php
include the page requested from the get variable (home, about, contact, etc.)
include footer.php
I liked this because the entire site did not have to reload when the user navigated from page to page.
I can't figure out how do this with Codeiginiter. Should I be using a controller for each page or one controller with a function for each page? Do anyone know of a good tutorial that does something similar? All the tutorials I've seen reload the entire site for every page.
Edit: Essentially I want to do this but with Codeigniter
Since it looks like you want relatively static content, but loaded dynamically you can do with one controller and (maybe) one method in the controller.
To do it with one method, do this in the welcome controller:
public page( $page_id ){
// views/header.php
$this->load->view( "header" );
if( $page_id = "about" ){
$this->load->view("about"); // views/about.php
}
else if( $page_id = "contact" ){
$this->load->view("contact"); // views/contact.php
}
// views/footer.php
$this->load->view("footer");
}
This takes a single get variable and figures out what page to load in between the header and footer.
This way www.yoursite.com/page/about will load the about page, www.yoursite.com/page/contact will load the contact page
Now, if you want to get rid of the /page part, you need to do some URL rerouting in application/config/routes.php
Alternatively you could use several methods in one controller:
public about( ){
// views/header.php
$this->load->view( "header" );
$this->load->view( "about" );
// views/footer.php
$this->load->view("footer");
}
public contact( ){
// views/header.php
$this->load->view( "header" );
$this->load->view( "contact" );
// views/footer.php
$this->load->view("footer");
}
Now your URLs look nicer without routing, but you have to load the header/footer for every page.
do you really like to copy/paste many $this->load->view() to any controller function?
It's a spaghetti code. You can try next: for example we have main.php controller as default controller. This main controller contain main function:
public function index()
{
ob_start();
$this->load->model('mainmodel');
$data = $this->mainmodel->_build_blocks(); //return array with needed blocks (header, menu, content, footer) in correct order
foreach ($data->result_array() as $row) {
$this->load->module($row['block_name']);
$this->name = new $row['block_name'];
$this->name->index();
}
ob_end_flush();
}
So, each other controller also have index() function which can dispatch actions depends on url segments, prepare params etc.
Footer controller as example (I use Smarty as template engine):
public function index()
{
$this->mysmarty->assign('year', date("Y"));
$this->mysmarty->view('footer');
return true;
}
Content controller will have:
public function index()
{
$name = $this->uri->segment(1, 'index');
$act = $this->uri->segment(2, 'index');
$this->load->module($name);
$this->name = new $name;
$pageData = $this->name->_show($act);
if ($pageData)
{
$this->mysmarty->assign($name, $pageData);
}
$this->mysmarty->view($name);
}
Thats mean what if you want to show http://site.name/page/contactus , we do next:
1) main.php start cycle by needed blocks
2) firstly we show header.tpl by header controller
3) then we show menu
4) then we call content controller which parse url, found what he should call _show() function in Page controller and pass action='contactus' to it. _show() function can contain some switch/case construction which show templates depends of action name (contactus.tpl in this case)
5) in the end we show footer template
In such case we have flexible structure. All controllers should have index() functions and all controllers who can be called in content should have _show($act) function. Thats all.
In codeIgniter you can do that like this, you can load different views at the same time from your controller. for example:
for example in your navbar view you have a Contacts button in your menu that would look like this:
<a href='contacts'>Contacts</a>
In your controller:
public function contacts()
{
$this->load->view('header');
$this->load->view('navbar');
$this->load->view('sidebar');
$this->load->view('contacts_view');
$this->load->view('footer');
}
So we're assuming here that you have the following views already that is ready to be loaded (header.php, navbar.php, sidebar.php, contacts_view.php, footer.php).
UPDATE:
you don't need to have $_GET[] request, just provide the method name from your controller in the <a> anchor tag
in codeigniter i using template
first make template file in one folder with header.php, navbar.php, etc.
example : template.php
<?php
echo $this->load->view('header'); //load header
echo $this->load->view('navbar');//load navbar
echo $this->load->view('sidebar');//load sidebar
echo $this->load->view($body); //load dynamic content
echo $this->load->view('footer');//load footer
?>
second in controller
function index( ){
$data['body'] = 'home'; // cal your content
$this->load->view('template', $data);
}
Using <a name="form"></a> and then going to that position by Click here
In code-igniter, I'm trying to load a view and a specific part of that view ie., <a name="form"></a>. It can be easily done by redirecting to that page using the #name but that clears up the form validation errors.
if($this->form_validation->run() == false)
{
$this->add();
}
where add() is the function which loads the view.
public function add()
{
$data['main_content'] = "add_view";
$data['css_code'] = "add_view.css";
$this->load->view('template', $data);
}
Any thoughts? Thanks in advance!
Ah! Found the answer at last.
You just have to add the anchor to the URI in form_open()
<?php echo form_open('controller/function#anchor'); ?>
Reference Codeigniter Forum
I have done very much research on this topic but I cannot find how i can do this. I am trying to add data to the $data parameter in a view that is being called from the controller of another view. However, any data i add to the subview via the subcontroller cannot be accessed by the subview. However, when I try to pass data to the subview via the client view, it works just fine. Most of the fixes on SO seem to reference just calling the $key in $data['key'] rather than $data so that doesn't seem really relevant here...
I have two classes:
welcome.php - a page
welcomemenu.php - a set of controls intended to
be loaded into welcome.php
Here is my Client Controller (the page that it's on, welcome.php), which stores the return val from subview $welcomemenu in its own $data array...:
<?php
class Welcome extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
//echo 'this is the Welcome index function';
$data['clienttestdata'] = 'data from welcome.php!';
$data['welcomemenu'] = $this->load->view('welcome/welcomemenu', $data, true);
$this->load->helper('url');
$this->load->view('templates/header');
$this->load->view('pages/welcome', $data);
$this->load->view('templates/footer');
}
}
And here is the client view ("welcome_view.php" - seems simple enough. The $welcomemenu var is where I put the returns from my component class...):
<section id="allWelcomeContent" class="mainBody">
<header id="mainPageHdr" class=mainPageHdr>
<!-- other stuff from my header -->
</header>
<!-- this is where i want to put the welcome menu... -->
<section id="mainWelcomeContent" class="mainContent">
<div>
<?php echo $welcomemenu;?>
</div>
</section>
</section>
And here is the Controller for my sub-component welcomemenu.php:
<?php
class Welcomemenu extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$data['menu_items'] = array('About', 'Portfolio', 'Resume', 'Fun', 'Blog');
$data['testdata'] = 'data from welcomemenu.php!';
$this->load->view('welcome/welcomemenu', $data);
}
}
And lastly: Here is the sub-view that is supposed to get data from its own controller, but cannot, even though it can take data from a calling client (i.e., the $clienttestdata shows up fine but $testdata doesn't)!
<section>
<!-- TODO: make this element repeatable so content can load from controller and/or model. -->
<div id="divMenuItems">
<?php echo $clienttestdata;?>
<?php echo $testdata;?>
</div>
</section>
Still i couldn't find any proper solution. if anyone then please give me
When you're including the welcomemenu partial in your Welcome/index method, you have to remember that the view does not go through its own controller. Instead, its contents are returned as a string and stored as a parameter. It gets all of its own parameters through the ones you send to it via $data:
$data['welcomemenu'] = $this->load->view('welcome/welcomemenu', $data, true);
This view will thus have access to everything in $data so far - nothing extra is added through the Welcomemenu controller. So, in the above case, it will have:
array
(
'clienttestdata' => 'data from welcome.php!'
)
If you add the parameters you need to $data (as $data['testdata']), your sub-view will have what it needs.