I need a way to pass a variable from one controller to the view that has been loaded from within another controller.
I want to put a login form on the homepage and evaluate the $error variable to see if it is equal to 1 and based on that display the error message.
controllers/posts.php:
<?php
class Posts extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('post');
$this->load->library('pagination');
}
function index($start=0){
$this->session->unset_userdata('pretrazi');
$data['posts']=$this->post->get_posts(1,$start);
$config['base_url']=base_url().'posts/index/';
$config['total_rows']=$this->post->get_posts_count();
$config['per_page']=1;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['prev_link'] = '<';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#"><span class="sr-only">(current)</span>';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$this->pagination->initialize($config);
$data['pages']=$this->pagination->create_links();
$this->load->view('posts_index',$data);
}
function post($slug){
$data['post']=$this->post->get_post($slug);
$this->load->view('post',$data);
}
function new_post(){
if($_POST){
$data=array(
'title'=>$_POST['title'],
'post'=>$_POST['post'],
'active'=>1,
'slug' => url_title($this->input->post('title'), 'dash', TRUE)
);
$this->post->insert_post($data);
redirect(base_url().'posts/');
}else{
$this->load->view('new_post');
}
}
function editpost($postid){
$data['success']=0;
if($_POST){
$data_post=array(
'title'=>$_POST['title'],
'post'=>$_POST['post'],
'slug' => url_title($this->input->post('title'), 'dash', TRUE)
);
$this->post->update_post($postid,$data_post);
$data['success']=1;
}
$data['post']=$this->post->get_post_by_id($postid);
$this->load->view('edit_post',$data);
}
function deletepost($postid){
$this->post->delete_post($postid);
redirect(base_url().'posts');
}
?>
controllers/users.php:
<?php
class Users extends CI_Controller{
function login(){
$data['error']=0;
if($_POST){
$this->load->model('user');
$username=$this->input->post('username',true);
$password=$this->input->post('password',true);
$user=$this->user->login($username,$password);
if(!$user){
$data['error']=1;
}else{
$this->session->set_userdata('userID',$user['userID']);
$this->session->set_userdata('username',$user['username']);
redirect(base_url().'posts');
}
}
}
function logout(){
$this->session->sess_destroy();
redirect(base_url().'posts');
}
}
?>
views/posts_index.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<div id="header">
<?php
if($this->session->userdata('username')){
?>
<p>Welcome <?php echo $this->session->userdata('username')?></p>
<a href="<?=base_url()?>users/logout">Logout</a>
<?php }else{?>
<?php
if(isset($error) and $error==1){
echo "Password/username wrong";
}?>
<form action="<?=base_url()?>users/login" method="POST">
Username:<input type="text" name="username"><br>
Password:<input type="password" name="password"><br>
<input type="submit" value="Log in">
</form>
<?php } ?>
</div>
<div id="container">
<h1>My blog</h1>
<?php
if(!isset($posts)){
?>
<p>There are no posts</p>
<?php
}else{
foreach($posts as $row){
?>
<h2><a href="<?php echo base_url()?>posts/post/<?php echo $row['slug']?>"><?php echo $row['title']?></a><?php if($this->session->userdata('username')){?>-<a href="<?php echo
base_url()?>posts/editpost/<?php echo $row['postID']?>">Edit</a> | <a class="delete" href="<?php echo base_url()?>posts/deletepost/<?php echo $row['postID']?>">Delete</a><?php }?></h2>
<p><?php echo substr(strip_tags($row['post']),0,200).".."?></p>
<p><a href="<?php echo base_url()?>posts/post/<?php echo $row['slug']?>">Read more</a></p>
<hr>
<?php
}
}
?>
</div>
<?php echo $pages;?>
<script>
$(function() {
$("a.delete").click(function() {
return confirm("Are you sure you want to delete this?");
});
});
</script>
</body>
</html>
Whenever provide wrong login credentials or just click the login button without entering anything i get a blank page.How can i get this $error to display above the form on the posts_index view which is my homepage???
Just set a flashdata in the session.
Try this: In your users.php controller after the login succes check set the flash data.
if(!$user)
{
$data_err = array(
'log_failed' =>'Invalid Login!',
'log_user' => $username
);
$this->session->set_flashdata('log_status',$data_err);
redirect(base_url());
}
and in the view :
if (null != $this->session->flashdata ( 'log_status' ))
{
$errorData = $this->session->flashdata ( 'log_status' );
echo $errorData ["log_failed"];
}