php – codeigniter ajax表单validation

嗨,我对jquery -ajax很陌生,我想要一些帮助,请加入CI。

我已经按照本教程使用AJAX提交表单,我想将此function添加到我的CodeIgniter站点。 我想要做的是当用户提交表单时,如果有任何validation错误在每个输入字段上单独显示(如在本机ci进程中),或者如果通过validation_errors()函数无法做到这一点。 如果没有错误,则在表单上方显示成功消息。

到目前为止,这是我的代码:

我的看法

// If validation succeeds then show a message like this, else show errors individually or in validation_errors() in a list 
Success!
'ajax-form')); ?>

<input type="text" name="product_name" value="product_name); ?>" />

<input type="text" name="brand" value="brand); ?>" />

...

我的控制器

  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请求传递我的validation结果(成功或发布错误)并在我的视图中显示它们? 从我做过的一些小研究中我发现你可以使用单个控制器,它同时包含原生进程和ajax请求(而不是使用2个控制器),但我的主要困难是,我不明白结果如何validation将通过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); }; } 

但是我遇到了一些问题

  1. 虽然我在没有错误的情况下将来自validation_errors()的错误消息作为警报错误获取,但我在警报错误中也得到了错误,并且警告成功。

2.我应该如何回复成功消息? 例如。 一条消息说“保存已完成!”。

  1. 在非ajax请求中,数据存储在数据库中,以防ajax不存储。 有什么想法可能有什么不对?

HTML:

 

Javascript ajax:

  success: function(response) { $('#ajaxResults').text(response); } 

你写的这个脚本只有在validation成功时才对,对吧?

错误。 每当您从服务器获得响应时,“执行”中的代码都会被执行(假设HTTP标头为200)。 你的javascript是否知道服务器是否有任何错误? 没有。

您需要JavaScript来识别validation是否失败或成功。 你有很多方法可以做到这一点。 其中一个可能是将消息发送到显示后跟0或1。

所以你的PHP看起来像:

 return "0 " . $errorMessage; 

 return "1 " . $successMessage; 

然后你的javascript应该用if语句和substring识别,如果消息以0或1开头。

使用这种方式我希望这对你有用