I am trying to validate a login form with CodeIgniter and jquery. I'm using the form_validation
library of CodeIgniter. When the user clicks the login button the form is submitted and the jquery function is called.If there's anything wrong with the data, the controller function passes the error messages as json
to the jquery function. The problem is that the error messages are not being displayed in the form. The only thing that shows up is the json array in a blank page.
This is the html form
:
<form class="col-md-12 center-block" action='<?php echo base_url();?>hyr/proceso' method='post' name='proceso' id='hyrForm'>
<div class="form-group">
<div class="mesazhe"></div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-envelope"></span>
</div>
<input type="text" name='email' id='email'class="form-control input-lg" placeholder="E-mail" >
</div>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-lock"></span>
</div>
<input type="password" name='fjalekalim' id='fjalekalim' class="form-control input-lg" placeholder="Fjalekalimi">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-block btn-lg btn-primary" onclick="hyr()">Hyr</button>
</div>
</form>
This is the jquery
function:
function hyr()
{
$(".text-danger").remove();
$(".form-group").removeClass('has-error').removeClass('has-success');
$("#hyrForm").unbind('submit').bind('submit', function() {
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data: form.serialize(),
dataType: 'json',
success:function(response) {
if(response.sukses===false) {
$('.text-danger').remove();
if(response.mesazhe instanceof Object) {
$.each(response.mesazhe, function(index, value) {
var id = $("#"+index);
id
.closest('.form-group')
.removeClass('has-error')
.removeClass('has-success')
.addClass(value.length > 0 ? 'has-error' : 'has-success')
.after(value);
});
} else {
$(".mesazhe").html('<div class="alert alert-warning alert-dismissible" role="alert">'+
'<button type="button" class="close" data-dismiss="alert" aria-label="Mbylle"><span aria-hidden="true">×</span></button>'+
'<strong> <span class="glyphicon glyphicon-exclamation-sign"></span> </strong>'+response.mesazhe+
'</div>');
}
}
}
});
});
}
Part of the controller function:
$validues = array('sukses' => false, 'mesazhe' => array());
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters('<p class="text-
danger">','</p>');
if($this->form_validation->run() === true) {
$rezultati = $this->validim->validim();
if($rezultati){
$e=$this->input->post('email');
if($e=='admin@yahoo.com'){
redirect('lista_perdoruesve');
}
else{
redirect('perdorues_faqjaKryesore');
}
}
else{
$validues['sukses'] = false;
$validues['mesazhe'] = "Nuk ekziston asnje perdorues me keto te dhena!";
}
}
else{
$validues['sukses'] = false;
foreach ($_POST as $key => $value) {
$validues['mesazhe'][$key] = form_error($key);
}
}
echo json_encode($validues);