[英]php - codeigniter ajax form validation
嗨,我是jquery -ajax的新手,我想寻求帮助,请加入CI。
我已经按照本教程通过AJAX提交表单的方式进行了学习 ,我想将此功能添加到我的CodeIgniter网站上。 我想做的是,当用户提交表单时,是否有任何验证错误要在每个输入字段上单独显示(如在本机ci进程中),或者如果不能通过validate_errors()函数来完成。 如果未发生错误,则在表格上方显示成功消息。
到目前为止,这是我的代码:
我的观点
// If validation succeeds then show a message like this, else show errors individually or in validation_errors() in a list
<div class="alert alert-success">Success!</div>
<?php echo validation_errors(); //show all errors that ajax returns here if not individualy ?>
<?php echo form_open('admin/product/add, array('class' => 'ajax-form')); ?>
<p>
<label for="product_name">Product *</label>
<input type="text" name="product_name" value="<?php echo set_value('product_name', $prod->product_name); ?>" />
<?php echo form_error('product_name'); ?>
</p>
<p>
<label for="brand">Brand</label>
<input type="text" name="brand" value="<?php echo set_value('brand', $prod->brand); ?>" />
<?php echo form_error('brand'); ?>
</p>
...
我的控制器
public function add($id){
// set validation rules in CI native
$rules = $this->product_model->rules;
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() === true) {
// get post data and store them in db
$data = $this->input_posts(array('product_name', 'brand', 'category_id', 'description'));
$this->product_model->save($data, $id);
// no errors - data stored - inform the user with display success-div
} else {
// validation failed - inform the user by showing the errors
}
//load the view
$this->load->view('admin/products/add', $data);
}
这是js脚本
$(document).ready(function () {
$('form.ajax-form').on('submit', function() {
var obj = $(this), // (*) references the current object/form each time
url = obj.attr('action'),
method = obj.attr('method'),
data = {};
obj.find('[name]').each(function(index, value) {
// console.log(value);
var obj = $(this),
name = obj.attr('name'),
value = obj.val();
data[name] = value;
});
$.ajax({
// see the (*)
url: url,
type: method,
data: data,
success: function(response) {
console.log(response); // how to output success or the errors instead??
}
});
return false; //disable refresh
});
});
我应该如何通过ajax请求传递验证结果(成功还是发布错误)并将其显示在我的视图中? 通过一些小小的研究,我发现您可以使用一个控制器,该控制器既可以处理本机进程也可以处理ajax请求(而不是使用两个控制器),但是我的主要困难是,我不知道结果如何验证将通过js脚本并将其显示在我的视图中? 请注意,我不想在警报框中显示任何内容,而是将结果显示在div上或将错误单独显示(如果可能)。
编辑我对我的应用程序做了一些更改,这是到目前为止的代码:
控制器
public function manage($id = NULL){
$this->load->library('form_validation');
$data['categ'] = $this->category_model->with_parents();
//fetch a single product or create(initialize inputs empty) a new one
if (isset($id) === true) {
$data['prod'] = $this->product_model->get($id);
$data['attr'] = $this->attribute_model->get_by('product_id', $id, null, true);
} else {
$data['prod'] = $this->product_model->make_new();
$data['attr'] = $this->attribute_model_model->make_new();
}
if (isset($_POST['general_settings'])) {
if ($this->form_validation->run('product_rules') === true) {
// get post inputs and store them in database
$data = $this->product_model->input_posts(array('product_name', 'brand', 'category_id', 'general_description'));
$this->product_model->save($data, $id);
$status = true;
} else {
// validation failed
$status = validation_errors();
}
if ( $this->input->is_ajax_request() ) {
echo json_encode($status);
exit;
}
redirect('admin/product');
}
//if (isset($_POST['attributes_settings'])) { the same thing here }
// load the view
$this->load->view('admin/products/manage', $data);
}
和js
success: function(response) {
//console.log(response);
if (data.status === true) {
$('#ajaxResults').addClass('alert alert-success').html(response);
} else {
$('#ajaxResults').addClass('alert alert-error').html(response);
};
}
但是我有一些问题
2.我也应该返回成功消息吗? 例如。 一条消息,说“保存已完成!”。
HTML:
<div id="ajaxResults"></div>
Javascript Ajax:
success: function(response) {
$('#ajaxResults').text(response);
}
您编写的此脚本仅在验证成功的情况下对吗?
错误。 每当您从服务器返回响应时,“成功”中的代码就会执行(假设HTTP标头为200)。 您的JavaScript是否知道服务器是否对您有任何错误? 没有。
您需要JavaScript才能识别验证是否成功。 您有很多方法可以做到这一点。 其中之一可能是发送要显示的消息,后跟0或1。
因此,您的PHP将如下所示:
return "0 " . $errorMessage;
和
return "1 " . $successMessage;
然后,如果消息以0或1开头,则JavaScript应使用if语句和substring
进行识别。
用这种方式我希望这对你有用
<script type='text/javascript'>
var base_url = '<?=base_url()?>';
function ajax_call()
{
var ids = $("#all_users").val();
$.ajax({
type:"POST",
url: base_url+"expense/home/get_expense",
data: "userid=" + ids,
success: function(result){
$("#your_div_id").html(result);
}
});
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.