1
I got the following jQuery
$(document).ready(function(){
$('#email').focus();
$("#formLogin").validate({
rules :{
email: { required: true, email: true},
senha: { required: true}
},
messages:{
email: { required: 'Campo Requerido.', email: 'Insira Email válido'},
senha: {required: 'Campo Requerido.'}
},
submitHandler: function( form ){
var dados = $( form ).serialize();
$.ajax({
type: "POST",
url: "<?php echo base_url();?>entrega/verificarLogin?ajax=true",
data: dados,
dataType: 'json',
success: function(data)
{
if(data.result == true){
window.location.href = "<?php echo base_url();?>entrega";
}
else{
$('#call-modal').trigger('click');
}
}
});
return false;
},
errorClass: "help-inline",
errorElement: "span",
highlight:function(element, errorClass, validClass) {
$(element).parents('.control-group').addClass('error');
},
unhighlight: function(element, errorClass, validClass) {
$(element).parents('.control-group').removeClass('error');
$(element).parents('.control-group').addClass('success');
}
});
});
And I have the Check login method, in Codeigniter.
public function verificarLogin(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','valid_email|required|xss_clean|trim');
$this->form_validation->set_rules('senha','Senha','required|xss_clean|trim');
$ajax = $this->input->get('ajax');
if ($this->form_validation->run() == false) {
if($ajax == true){
$json = array('result' => false);
echo json_encode($json);
}
else{
$this->session->set_flashdata('error','Os dados de acesso estão incorretos.');
redirect($this->login);
}
}
else {
$email = $this->input->post('email');
$senha = $this->input->post('senha');
$this->load->library('encrypt');
$senha = $this->encrypt->sha1($senha);
$this->db->where('email',$email);
$this->db->where('senha',$senha);
$this->db->where('status',1);
$this->db->limit(1);
$usuario = $this->db->get('usuario')->row();
$permissao = $usuario->idPermissao;
$sql = "SELECT * FROM permissoes WHERE idPermissao = '{$permissao}'";
$permissao = $this->db->query($sql)->row('permissoes');
if(count($usuario) > 0){
$dados = array('nome' => $usuario->nome, 'empresa' => $usuario->empresa, 'id' => $usuario->idUsuario, 'idCliente' => $usuario->idCliente, 'permissao' => $usuario->idPermissao, 'permissoes' => $permissao, 'logado' => TRUE);
$this->session->set_userdata($dados);
if($ajax == true){
$json = array('result' => true);
echo json_encode($json);
}
else{
redirect(base_url().'entrega');
}
}
else{
if($ajax == true){
$json = array('result' => false);
echo json_encode($json);
}
else{
$this->session->set_flashdata('error','Os dados de acesso estão incorretos.');
redirect($this->login);
}
}
}
}
And the HTML:
<form class="form-vertical" id="formLogin" method="post" action="<?php echo base_url()?>entrega/verificarLogin">
<?php if($this->session->flashdata('error') != null){?>
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">×</button>
<?php echo $this->session->flashdata('error');?>
</div>
<?php }?>
<div class="control-group normal_text"> <h3><img src="<?php echo base_url()?>assets/img/logo-home.png" alt="Logo" /></h3></div>
<div class="control-group">
<div class="controls">
<div class="main_input_box">
<span class="add-on bg_lg"><i class="icon-user"></i></span><input id="email" name="email" type="text" placeholder="Email" />
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="main_input_box">
<span class="add-on bg_ly"><i class="icon-lock"></i></span><input name="senha" type="password" placeholder="Senha" />
</div>
</div>
</div>
<div class="form-actions" style="text-align: center">
<button class="btn btn-info btn-large"/> Logar</button>
</div>
</form>
When entering, enter normal if the password is correct. If I’m wrong, the message doesn’t come... and I can’t find the error.