Reputation: 1239
I have a little problem with my code. I am studying bootstrap CSS. And I am so amaze with this framework. That's why I decided to study this. I don't really don't have enough knowledge in CSS. But I understand a little.
I am creating a form and I want to have a validation message if username or password are wrong.. I have a validation of required. But after creating my validation and passing to the model. I don't know how can I display again to the same page and display my error message?
Here's my simple code.
In my controller
class Admin extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->model('admin/adminauth');
}
public function index() {
$data['title'] = "Administration";
$this->form_validation->set_rules('username', 'Username', 'required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('header/header',$data);
$this->load->view('contents/login',$data);
$this->load->view('footer/footer');
} else {
$validate_login = $this->adminauth->validate_login();
if($validate_login > 0) {
} else {
//this should be go back again to the index and display my error message.
$this->index();
}
}
}
}
In my view I have this
<div class="panel-body form-admin-signin">
<?php
$form_array = array(
'class' => 'form-signin'
);
echo form_open('admin',$form_array);
?>
<div class="form-group">
<h3 class="form-signin-heading">Username</h3>
<input type="text" name="username" class="form-control inputs" placeholder="username" value="<?php echo set_value('username'); ?>" />
<?php echo form_error('username','<div class="error_msg">','</div>'); ?>
<h3 class="form-signin-heading">Password</h3>
<input type="password" name="password" class="form-control inputs" placeholder="password" value="" />
<?php echo form_error('password','<div class="error_msg">','</div>'); ?>
<div class="checkbox">
<label><input type="checkbox" /> Remember me</label>
</div>
<input type="submit" class="btn btn-primary login-btn" value="Login" />
</div>
<?php echo form_close(); ?>
<?php
//THIS IS I WANT TO DISPLAY. IF INVALID THE CHECKING IN SQL IT WILL DISPLAY BELOW THE FORM
$validate = '';
if($validate == 1) {
?>
<div class="alert alert-danger">
<label>Invalid Username or Password please try again!</label>
</div>
<?php
}
?>
</div>
In my model I have this
class AdminAuth extends CI_Model {
public function __construct() {
parent::__construct();
}
public function validate_login() {
$username = $this->input->post('username');
$password = md5($this->input->post('password'));
$query = "SELECT username FROM user WHERE username = '".$username."' AND password = '".$password."' LIMIT 1";
$get_result = $this->db->query($query);
return $get_result->num_rows();
}
}
That's all guys, I hope you can help me. Or can you give me an example using AJAX with this?
Upvotes: 2
Views: 3962
Reputation: 9468
You can create a $data['errors']
variable in your controller within
if ($this->form_validation->run() == FALSE) {
add
$data['errors'] = validation_errors();
and at the top of your view file add
<?php if(isset($errors)){ ?>
<div class="alert alert-danger"> <!--bootstrap error div-->
<?=$errors?>
</div>
<?php } ?>
Upvotes: 0
Reputation: 13728
try
$this->session->set_flashdata('error', 'Error while sending enquiry email.');
$this->index();
or you can use :-
$this->session->set_flashdata('error', 'Error while sending enquiry email.');
redirect('contoller');
on view :-
<?php echo $this->session->flashdata('error');?>
Upvotes: 2