我想在html弹出窗口中使用ci中的表单验证库,并在客户端请求时向客户端显示错误,如果验证错误==数据库中的真正更新行,如果验证错误= False,则向客户端显示字段错误
我的html弹出代码:
<div class="row">
<div class="form-group col-lg-6">
<label class="col-sm-4 control-label">tarh</label>
<div class="col-sm-8 control-label">
<input type="text" name="modal_tarh" id="modal_tarh" class="form-control">
</div>
</div>
<div class="form-group col-lg-6">
<label class="col-sm-3 control-label">agreement number</label>
<div class="col-sm-6 control-label">
<input type="text" name="modal_agreement_number" id="modal_agreement_number" class="form-control">
</div>
</div>
</div>
<button type="button" class="btn btn-info" id="edit_agreement">edit</button>
ajax代码:
$(document).ready(function() {
$("#edit_agreement").click(function () {
var String_Url = "address";
var String_Method = "POST";
data_send = {
'f1':$("#modal_tarh").val(),
'f2':$("#modal_agreement_number").val()
};
$.ajax({
url:String_Url,
type:String_Method,
data:data_send,
contentType: 'application/json',
success:function(callbackData, status, xhr){
alert(callbackData)
console.log(callbackData)
},
error:function(xhr, status, err){
alert(xhr.status);
}
});
});
});
codeigniter控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Portal extends My_controller
{
public function editAgreement()
{
$this->load->library('form_validation');
$config = array(
array(
'field' => 'modal_tarh',
'label' => 'tarh',
'rules' => 'required',
'errors' => array
(
'required' => 'pls fill %s .',
)
),
array(
'field' => 'modal_agreement_number',
'label' => 'agreement number',
'rules' => 'required',
'errors' => array
(
'required' => 'pls fill %s .',
)
)
);
$this->form_validation->set_rules($config);
if ($this->form_validation->run() == FALSE)
{
echo validation_errors();
}
else
{
redirect('admin/portal/');
}
}
}
ajax中的callbackData包含http状态代码200和无数据
答案 0 :(得分:0)
您无法通过ajax调用进行重定向。
更改
redirect('admin/portal/');
到
echo 'Success';
或者,如果您确实想要成功重定向ajax请求,请执行
echo base_url('admin/portal/');
并在你的ajax请求中:
success:function(callbackData, status, xhr){
window.location = callbackData;
},
您还可以考虑让控制器同时处理ajax和非Ajax请求:
if ($this->input->is_ajax_request()) {
echo base_url('admin/portal/');
} else {
redirect('admin/portal/');
}