我需要一些帮助。
- 从我的测试到现在,我得到了这些:
- 如果验证失败 - 我视图中的 div #ajaxResults 获取类 alert-error 并显示错误(如预期的那样)。
- 如果验证成功 - div #ajaxResults 获取类 alert-error(非预期)并显示 true(如预期)。
我想做的是当有一个ajax请求时:
- 如果验证成功 - 返回带有类 alert-success(bootstrap) 的 div #ajaxResults 并显示一条消息“数据成功存储在数据库中”。我无法弄清楚我应该如何返回以及来自控制器的消息。
- 如果验证未能显示带有验证错误的类警报错误的 div #ajaxResults。(原样)
2.我还注意到,在非ajax请求的情况下,数据存储在数据库中,但在ajax请求的情况下则不存储。我该如何解决这个问题才能正常工作?
这是我的控制器
public function manage($id = NULL){
$this->layout->add_includes('js/ckeditor/ckeditor.js')->add_includes('js/ajax_scripts.js');
$this->load->library('form_validation');
$data['categ'] = $this->category_model->with_parents();
//fetch a single product or create a new one
if ( isset($id) ) {
$data['prod'] = $this->product_model->get($id);
$data['attr'] = $this->attributes_model->get_by('product_id', $id);
} else {
$data['prod'] = $this->product_model->make_new();
$data['attr'] = $this->attribute_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','visible'));
$this->product_model->save($data, $id);
$result = array('status' => 200, 'message' => 'data stored in database successfully');
} else {
// validation failed
$result = array('status' => 400, 'reason' => validation_errors());
}
if ( $this->input->is_ajax_request() ) {
echo json_encode($result);
exit;
}
$this->session->set_flashdata('message', $result);
redirect('admin/product');
}
// if ( isset($_POST['attribute_settings']) ) { same goes here }
// load the view
$this->load->view('admin/products/manage', $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) {
var obj = $(this),
name = obj.attr('name'),
value = obj.val();
data[name] = value;
});
// console.log(data);
// data.general_settings = 1;
$.ajax({ // see the (*)
url: url,
type: method,
data: data,
dataType: 'json',
success: function(response) {
//console.log(response);
$('#ajaxResults').removeClass('alert alert-success alert-error');
$('.control-group').removeClass('error warning error info success');
if (response.status == 200) {
$('#ajaxResults').addClass('alert alert-success').html(response.message);
$('.control-group').addClass('success'); // ** apply it only on valid fields
} else {
$('#ajaxResults').addClass('alert alert-error').html(response.reason);
$('.control-group').addClass('error'); // ** apply it only on invalid fields
}
}
});
return false; //disable refresh
});
});
我在视图中使用 .ajax-form 类的表单之一
<?php echo form_open('admin/product/manage/'.$prod->product_id, array('class' => 'ajax-form')); ?>
<div class="control-group">
<label class="control-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'); ?>
</div>
<div class="control-group">
<label class="control-label" for="brand">Brand</label>
<input type="text" name="brand" value="<?php echo set_value('brand', $prod->brand); ?>" />
<?php echo form_error('brand'); ?>
</div>
// apply same to all my fields
<p>
<label for="category_id">Category *</label>
<?php echo form_dropdown('category_id', $categ);
echo form_error('category_id');
?>
</p>
<p>
<label for="general_description">General Description</label>
<textarea class="ckeditor" name="general_description" rows="10" cols="250"><?php echo set_value('general_description', $prod->general_description); ?></textarea>
<?php echo form_error('general_description') . PHP_EOL; ?>
</p>
<p>
<label for="visible">Visible</label>
<select name="visible" class="span1">
<option value="0">No</option>
<option value="1">Yes</option>
</select>
</p>
<p>
<button class="btn btn-primary" type="submit" name="general_settings">Ok</button>
</p>
<?php echo form_close() . PHP_EOL; ?>
任何帮助,将不胜感激。