how to flash error data on view in codeigniter - php

how to flash error on view in codeigniter.... i m creating login on
codeigniter but if user login value not equivalent to dataase table
value. then i need to show error ....invalid user name or
password.. for that i m using set_flashdata but.... on view when i
call that by error_message(). then error showing:
Call to undefined function error_message() .....
mylogin auth controller:
public function auth() {
$data['title'] = 'Heart Surgeon';
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_login');
if ($this->form_validation->run() == TRUE) {
$Value['admin_username'] = $this->input->post('username');
$Value['admin_password'] =md5($this->input->post('password'));
if ($Value != null) {
$result = $this->user_model->login($Value);
if($this->session->set_userdata($result) != null){
redirect('dashboard');
}
}
} else {
$this->index();
}
}
my model
public function login($value) {
$query = $this->db->get_where('tbl_admin', $value, 1);
if ($query->num_rows() > 0) {
$row = $query->row_array();
$sess_arr = array(
'admin_user' => $row['admin_username'],
'adm_key' => $row['admin_key'],
'admin_type' => $row['admin_type'],
'admin_id' => $row['admin_id'],
'admin_logged_in' => TRUE
);
$this->session->set_userdata($sess_arr);
}
else{
$this->session->set_userdata(array('msg_type'=>'error'));
$this->session->set_flashdata('error', 'Invalid username/password');
redirect('home');
}
}
my view
<?php if (!defined('BASEPATH')) exit('No direct script access
allowed'); ?> <?php $this->load->view('layout/header'); ?>
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4 text-center logo-margin ">
<img src="<?php echo base_url(); ?>assets/img/logo.png" alt=""/>
</div>
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Please Sign In</h3>
</div>
<div class="panel-body">
<?php if (validation_errors()) {
?>
<div class="warning" style="padding: 0px;">
<?php echo validation_errors(); ?>
</div>
<?php
}
$atts = array(
'width' => '650',
'height' => '400',
'scrollbars' => 'yes',
'status' => 'yes',
'resizable' => 'yes',
'screenx' => '0',
'screeny' => '0'
);
?>
<div><?php echo error_message();?></div>
<?php echo form_open('home/auth'); ?>
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="E-mail" name="username" type="email" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" value="">
</div>
<div class="checkbox">
<label>
<input name="remember" type="checkbox" value="Remember Me">Remember Me
</label>
</div>
<!-- Change this to a button or input when using this as a form -->
<input type="submit" value="login" class="btn btn-lg btn-success btn-block">
</fieldset>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div> </div> <?php $this->load->view('layout/footer'); ?>

you calling wrong function to get flash error
try set flash error on controller :-
$this->session->set_flashdata('error', 'Invalid username/password');
get error flashdata on view :-
<?php echo $this->session->flashdata('error');?>
For more :- http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

Basically when we set any name value pair using set_flashdata method(),
$this->session->set_flashdata('error', 'Invalid username/password');
it will automatically be discarded/removed from the session once it will be read using
$this->session->flashdata(); method.
I am 100% agreed with Rakesh.

Related

Codeigniter not validation login form

I'm already pretty good with codeigniter so I feel pretty confident with my code. However, my form validation is not working. Its never passing validation, and never showing any errors. I've dumped out the CI instance and can see my validations are set correctly, but no luck.
Here's the function in my controller
$this->load->helper(array('form', 'url', 'html'));
$this->load->library('form_validation');
$data = array();
$this->form_validation->set_rules('email', 'Email', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
$validation_errors = validation_errors();
if( $validation_errors !== FALSE ):
$data["validation_errors"] = $validation_errors;
endif;
if ($this->form_validation->run() == FALSE):
$this->load->view('common/head-auth', $data);
$this->load->view('pages/page-auth-login', $data);
$this->load->view('common/foot-auth');
else:
$this->load->model("site/users_model");
$this->users_model->login();
endif;
and here's the my view file
<main class="auth">
<header id="auth-header" class="auth-header"></header>
<?php
$atts = array("class" => 'auth-form');
echo form_open('auth/login', $atts);
?>
<div class="form-group">
<img src="<?php echo base_url( "assets/images/rplogo.png" ); ?>" alt="" width="100%">
</div>
<?php
if( isset( $validation_errors ) ):
$error_messages = "<div class=\"alert alert-danger alert-dismissible fade show\">";
$error_messages .= "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button>";
$error_messages .= $validation_errors;
$error_messages .= "</div>";
echo $error_messages;
endif;
?>
<div class="form-group">
<div class="form-label-group">
<input type="text" id="inputEmail" class="form-control" placeholder="Email Address" name="email" value="<?php echo set_value('email'); ?>">
<label for="inputUser">Email Address</label>
</div>
</div>
<div class="form-group">
<div class="form-label-group">
<input type="password" id="inputPassword" class="form-control" placeholder="Password" name="password">
<label for="inputPassword">Password</label>
</div>
</div>
<div class="form-group">
<button class="btn btn-lg btn-primary btn-block" type="submit">Log In</button>
</div>
<div class="text-center pt-3">
Forgot Password?
</div>
</form>
<footer class="auth-footer"> © 2019 All Rights Reserved. </footer>
and yes I have went through all other posts on this topic without a solution.
Thanks in advance
if ($this->form_validation->run() == FALSE):
$validation_errors = validation_errors();
if( $validation_errors !== FALSE ):
$data["validation_errors"] = $validation_errors;
endif;
$this->load->view('common/head-auth', $data);
$this->load->view('pages/page-auth-login', $data);
$this->load->view('common/foot-auth');
else:
$this->load->model("site/users_model");
$this->users_model->login();
endif;

session in codeigniter not working

The session is not working properly. when login, it is directing member_area function which is correct but in member_area function if the condition is not working. The result is directly going in else part of member_area function that is output dies. Is there any mistake in member_area function if condition?
This is my main controller i.e., Welcome controller
function login()
{
$this->form_validation->set_rules('email', 'E-mail', 'valid_email');
$this->form_validation->set_rules('password', 'Password', 'min_length[5]|max_length[8]');
if ($this->form_validation->run()==FALSE)
{
$data['view'] = 'login_view';
$this->load->view('load_view',$data);
}
else
{
$email=$_POST['email'];
$password = md5($_POST['password']);
$this->load->model('Sample_model');
$credentials = array(
'email' => $_POST['email'],
'password' => md5($_POST['password'])
);
$user = $this->Sample_model->check_user($credentials);
if($user->num_rows() == 1)
{
$user = $user->row();
$session = array(
'name' => $user->name,
'is_logged_in' => TRUE
);
$this->session->set_userdata($sesson);
redirect('welcome/member_area');
}
else {
$data['view'] = 'error_view';
$data['msg'] = 'Login failed';
$this->load->view('load_view',$data);
}
}
}
public function member_area()
{
if($this->session->userdata('is_logged_in'))
{
$data['view'] = 'memberarea_view';
$this->load->view('load_view',$data);
}
else die('die');
}
I have added session in config/Autoload
$autoload['libraries'] = array('database','session','form_validation');
This is the model used in the present login.
Sample_model
<?php
class Sample_model extends CI_Model {
public function __construct()
{
//Call the CI_Model constructor
parent::__construct();
}
public function add_user($user)
{
return $this->db->insert('users', $user);
}
public function check_user($credentials)
{
$this->db->where($credentials);
return $this->db->get('users');
}
}
This is login_view form. I am entering email and password from this form.
<section class="title">
<div class="container">
<div class="row-fluid">
<div class="span6">
<h1>Login</h1>
</div>
<div class="span6">
<ul class="breadcrumb pull-right">
<li>Home <span class="divider">/</span></li>
<li>Pages <span class="divider">/</span></li>
<li class="active">Login</li>
</ul>
</div>
</div>
</div>
</section>
<!-- / .title -->
<section id="login-page" class="container">
<form class="center" action='' method="POST">
<fieldset class="login-form">
<div class="control-group">
<!-- E-mail -->
<div class="controls">
<input type="text" id="email" name="email" placeholder="E-mail" class="input-xlarge" required="required">
<?php echo form_error('email'); ?>
</div>
</div>
<div class="control-group">
<!-- Password-->
<div class="controls">
<input type="password" id="password" name="password" placeholder="Password" class="input-xlarge" required="required">
<?php echo form_error('password'); ?>
</div>
</div>
<div class="control-group">
<!-- Button -->
<div class="controls">
<button class="btn btn-success btn-large btn-block">Login</button>
</div>
</div>
</fieldset>
</form>
</section>
<!-- /#login-page -->
Libraries
$autoload['libraries'] = array('database','session','form_validation');
helper
$autoload['helper'] = array('url', 'file');
Controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
function login()
{
$this->form_validation->set_rules('email', 'E-mail', 'valid_email');
$this->form_validation->set_rules('password', 'Password', 'min_length[5]|max_length[8]');
if ($this->form_validation->run()==FALSE)
{
$data['view'] = 'login_view';
$this->load->view('login_view',$data);
}
else
{
$email=$_POST['email'];
$password = md5($_POST['password']);
$this->load->model('Sample_model');
$credentials = array(
'email' => $_POST['email'],
'password' => md5($_POST['password'])
);
$user = $this->Sample_model->check_user($credentials);
if($user->num_rows() == 1)
{
$user = $user->row();
$session = array(
'name' => $user->name,
'is_logged_in' => TRUE
);
$this->session->set_userdata($session);
redirect('welcome/member_area');
}
else {
$data['view'] = 'error_view';
$data['msg'] = 'Login failed';
$this->load->view('login_view',$data);
}
}
}
public function member_area()
{
if($this->session->userdata('is_logged_in'))
{
$data['view'] = 'member_view';
$this->load->view('member_view',$data);
}
else{ die('die');
}
}
function logout()
{
$newdata = array(
'name' =>'',
'is_logged_in' => FALSE,
);
$this->session->unset_userdata($newdata);
$this->session->sess_destroy();
redirect('welcome/login','refresh');
}
}
Model
class Sample_model extends CI_Model {
public function __construct()
{
//Call the CI_Model constructor
parent::__construct();
}
public function add_user($user)
{
return $this->db->insert('users', $user);
}
public function check_user($credentials)
{
$this->db->where($credentials);
return $this->db->get('users');
}
}
Login View
<div class="control-group">
<!-- E-mail -->
<div class="controls">
<input type="text" id="email" name="email" placeholder="E-mail" class="input-xlarge" required="required">
<?php echo form_error('email'); ?>
</div>
</div>
<div class="control-group">
<!-- Password-->
<div class="controls">
<input type="password" id="password" name="password" placeholder="Password" class="input-xlarge" required="required">
<?php echo form_error('password'); ?>
</div>
</div>
<div class="control-group">
<!-- Button -->
<div class="controls">
<button class="btn btn-success btn-large btn-block">Login</button>
</div>
</div>
</fieldset></form></section>
Member View
Welcome Member

Codeigniter Form Won't Insert Data Into Database

I have a web app built on Codeigniter but I don't know which version it is. My job is just adding a simple form for this web app. The form should look like this:
And then here's my VIEW:
<div class="box">
<h4 class="white">ADD CIT Details</h4>
<div class="box-container">
<div class="modal-body form">
<form action="#" id="formcit" class="form-horizontal">
<?php echo form_open('insert_cit_ctrl'); ?>
<div class="form-body">
<?php if (isset($message)) { ?>
<CENTER><h3 style="color:green;">Data inserted successfully</h3></CENTER><br>
<?php } ?>
<div class="form-group">
<label class="control-label col-md-3">Terminal ID</label> <?php echo form_error('dterminalid'); ?>
<div class="col-md-9">
<?php echo form_input(array('id' => 'dterminalid', 'name' => 'dterminalid')); ?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">CIT</label>
<div class="col-md-9">
<select class="form-control" id="selectcit" >
<option value="none">-- Select CIT --</option>
<option value="CMS">1. CMS</option>
<option value="ALPHA">2. ALPHA</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Branch</label> <?php echo form_error('dbranch'); ?>
<div class="col-md-9">
<?php echo form_input(array('id' => 'dbranch', 'name' => 'dbranch')); ?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">COMM</label> <?php echo form_error('dcomm'); ?>
<div class="col-md-9">
<?php echo form_input(array('id' => 'dcomm', 'name' => 'dcomm')); ?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Machine Type</label> <?php echo form_error('dmachinetype'); ?>
<div class="col-md-9">
<?php echo form_input(array('id' => 'dmachinetype', 'name' => 'dmachinetype')); ?>
</div>
</div>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
</div>
<?php echo form_close(); ?>
</form>
</div>
</div><!-- end of div.box-container -->
</div> <!-- end of div.box -->
And then this is my controller:
function insert_cit_ctrl() {
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('dterminalid', 'Terminal ID', 'required');
$this->form_validation->set_rules('dcit', 'CIT', 'required');
$this->form_validation->set_rules('dbranch', 'Branch', 'required');
$this->form_validation->set_rules('dcomm', 'COMM', 'required');
$this->form_validation->set_rules('dmachinetype', 'Machine Type', 'required');
if ($this->form_validation->run() == FALSE) {
echo "<script>alert('Something wrong with your input');</script>";
} else {
//Setting values for tabel columns
$data = array(
'terminal_id' => $this->input->post('dterminalid'),
'cit' => $this->input->post('dcit'),
'branch' => $this->input->post('dbranch'),
'comm' => $this->input->post('dcomm'),
'machine_type' => $this->input->post('dmachinetype')
);
//Transfering data to Model
$this->cit_model->modeladdcit($data);
$data['message'] = 'Data Inserted Successfully';
//Loading View
$this->load->view('template', $data);
}
}
And here's my model:
<?php
class Cit_model extends CI_Model{
function __construct() {
parent::__construct();
}
function modeladdcit($data){
$this->load->database('database_office', 'TRUE');
$this->db->insert('tbl_cit', $data);
}
}
?>
And the result? Here's my database:
There's no error at all. It's just that after I submit the data, it won't appeared on my database, not a single data. Meaning, that data is not even INSERT INTO the database.
I don't understand. What is wrong with my code?
UPDATE:
Ok, so I followed your tips guys, like this:
<form action="<?php echo base_url("evangelion/insert_cit_ctrl"); ?>" id="formcit" method="POST" class="form-horizontal">
What actually happened was... NOTHING. I mean, the data still not inserted into the database. And worse, the page ruined, it called only the function, not the whole page like it used to.
add the action and method to your form
<form action="<?php echo base_url("controller_name/function_name"); ?>" id="formcit" method="POST" class="form-horizontal">
First try to display errors like this
you can find where you are wrong..
where you call controller in codeigniter you must call controller
action="controllername/actionname"
<?php
echo ini_get('display_errors');
if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}
echo ini_get('display_errors');
?>
i thought you did not mention method type in form
method ="POST"

how to use required in form_validation in ci?

Hi when I use required in my rules in ci form_validation, I know the value of this required input post and don't show any message .but this if ($this->form_validation->run()) is always false.
thanks for your help.
my view:
<form>
<div class="row">
<div class="form-group">
<div class="col-md-2"><label>Name</label></div>
<div class="col-md-10 col-md-pull-1">
<input name="Name" class="form-control" id="Name"
value="<?php echo set_value('Name')?>"
autocomplete="off"></input>
<?php echo form_error('Name'); ?>
</div>
</div>
</div>
</form>
my controller:
public function show_info()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('Name', 'Name', 'required');
$this->form_validation->set_message('required'," please enter %s");
if ($this->form_validation->run()== FALSE)
{
echo "false";
}
else redirect();
}
In your view file, you had wrote nmae except name
<div class="row">
<div class="form-group">
<div class="col-md-2"><label>Name</label></div>
<div class="col-md-10 col-md-pull-1">
<input name="Name" class="form-control" id="Name"
value="<?php echo set_value('Name')?>"
autocomplete="off"></input>
<?php echo form_error('Name'); ?>
</div>
</div>
</div>
correct it.
please load again view file after false form_validation
public function show_info()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('Name', 'Name', 'required');
$this->form_validation->set_message('required'," please enter %s");
if ($this->form_validation->run()== FALSE)
{
echo "false";
$this->load->view('view_file','',true);
}
else redirect();
}

codeigniter- on change password display change password successful

My code so far is working and changing the user password. I would now like to display the user that their password was changed successfully else say try again but i cant make it happen. Any help would be appreciated, thanks.
Here is the controller:
public function change_password($arg = "admin")
{
if(isset($_POST['data']) && !empty($_POST['data']))
{
$this->load->library('form_validation');
$this->form_validation->set_rules('data[oldpassword]', 'old password', 'required');
$this->form_validation->set_rules('data[password]', 'password', 'trim|required');
$this->form_validation->set_rules('data[passconf]', 'password Confirmation', 'trim|required|md5|matches[data[password]]');
if ($this->form_validation->run() == FALSE)
{
$data['requestee'] = $arg;
$this->load->view('change_password', $data);
}
else
{
$data = $_POST['data'];
array_pop($data);
$data['requestee'] = $arg;
$data['email'] = $this->session->userdata('email');
//print_r($data); exit;
if($data['requestee'] == "admin")
{
$this->load->model('login_model');
if($this->login_model->updatepassword($data))
{
redirect('/admin/show', 'refresh');
}
else
{
redirect('login/change_password/');
}
}
elseif($data['requestee'] == "org")
{
$this->load->model('login_model');
if($this->login_model->updatepassword($data))
{
redirect('/organizer/show', 'refresh');
}
else
{
redirect('login/change_password/org');
}
}
else
{
echo "could not change password";
}
}
}
else
{
$data = array('requestee' => $arg);
$this->load->view('change_password', $data);
}
}
Here is the view:
<form action="<?php echo site_url('login/change_password'); ?>" class="login-wrapper" method="post">
<div class="header">
<div class="row-fluid">
<div class="span12">
<h3>Change password<img src="<?php echo asset_url();?>img/logo1.png" alt="Logo" class="pull-right"></h3>
<p>Fill out the form below to change your password.</p>
</div>
</div>
</div>
<div class="content">
<div class="row-fluid">
<div class="span12">
<input class="input span12 password" id="" name="data[oldpassword]" placeholder="Old password" required="required" type="password" value="<?php echo set_value('oldpassword'); ?>">
</div>
</div>
<div class="row-fluid">
<div class="span12">
<input class="input span12 password" id="" name="data[password]" placeholder="New password" required="required" type="password" value="<?php echo set_value('password'); ?>">
</div>
</div>
<div class="row-fluid">
<div class="span12">
<input class="input span12 password" id="" name="data[passconf]" placeholder="Confirm new password" required="required" type="password" value="<?php echo set_value('passconf'); ?>" >
</div>
</div>
</div>
<div class="actions">
<input class="btn btn-danger" name="change_password" type="submit" value="Change" >
<div class="clearfix"></div>
</div>
</form>
It appears that after changing the users password, you are using a redirect to return the user to another page.
In your controller, just before doing the redirect, You can use CodeIgniters session data to set a message in flashdata like so:
$this->session->set_flashdata('message', 'Your Password Was Successfully Changed');
or
$this->session->set_flashdata('message', 'Your Password Was Not Changed');
Then in your view, you can add:
<?php echo $this->session->flashdata('message'); ?>
Please note that in order for this to work, if you have not done so already, you will need to either manually load the session library on every page where it is used or add it to your autoload.php file.

Categories