我正在尝试使用CodeIgniter和jquery验证登录表单。我正在使用CodeIgniter的form_validation
库。当用户单击登录按钮时,表单将被提交并调用jquery函数。如果数据有任何问题,控制器函数会将错误消息作为json
传递给jquery函数。问题是错误消息没有显示在表单中。唯一显示的是空白页面中的json数组。
这是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>
这是jquery
函数:
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>');
}
}
}
});
});
}
控制器功能的一部分:
$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);