我目前正在使用Codeigniter并在一个HTML表单中处理CRUD操作。 我正在使用Ajax进行创建/读取/更新。
我还使用事务管理作为数据库查询的最佳实践。
问题:
(1)我想要更新和插入错误的单独错误消息。 我没有进入ajax错误部分。
(2)我使用调试器来调试这个问题,但我没有把它弄好。
这是我的控制器的代码。
控制器 :
public function save_candidate_experience() { $this->db->trans_start(); if(empty($postId))){ $query_staus = $this->test_model->insert_function( $data ); if($query_staus != TRUE) { $msg = array('message'=>'Failed To Save! Erroe Wile Inserting.'); } else{ $msg = array('message'=>'Successfully Insert'); } } else { $query_staus2 = $this->test_model->update_function( $data ); if($query_staus2 != TRUE) { $msg = array('message'=>'Failed To Save! Erroe Wile Updateing.'); }else{ $msg = array('message'=>'Successfully Updated'); } } if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); echo json_encode ($msg); } else { $this->db->trans_commit(); echo json_encode ($msg); } }这是型号代码 :
public function insert_function() { $this->db->insert('table_name', $data); if($this->db->affected_rows() > 0){ return TRUE; } else{ return FALSE; } } public function update_function() { $this->db->where('id', $id); $this->db->update('test_table', $data); if($this->db->affected_rows() > 0){ return TRUE; } else{ return FALSE; } }我认为Ajax代码 。
$.ajax({ type: 'POST', async: true, dataType: 'Json', url: save_experience, data: $('#candidata_exp_form').serialize(), success: function (response) { //doing something with ajax success },error: function (msg) { alert(msg.message); // I know I can give alert message here but. //I don't want to give alert message here. //I want to indicate user that the error occure whilt insert or update seperately. } });I am currently using Codeigniter and working on CRUD operation in one HTML form. I am using Ajax for this create/read/update.
I have also used Transaction Management as best practices in a database query.
The Problem:
(1) I want separate Error message for Update and Insert Error. Which I did not get in the ajax error section.
(2) I have used the debugger to debug this problem but I do not get it proper.
Here is the Code of my controller.
Controller:
public function save_candidate_experience() { $this->db->trans_start(); if(empty($postId))){ $query_staus = $this->test_model->insert_function( $data ); if($query_staus != TRUE) { $msg = array('message'=>'Failed To Save! Erroe Wile Inserting.'); } else{ $msg = array('message'=>'Successfully Insert'); } } else { $query_staus2 = $this->test_model->update_function( $data ); if($query_staus2 != TRUE) { $msg = array('message'=>'Failed To Save! Erroe Wile Updateing.'); }else{ $msg = array('message'=>'Successfully Updated'); } } if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); echo json_encode ($msg); } else { $this->db->trans_commit(); echo json_encode ($msg); } }This is the Model Code:
public function insert_function() { $this->db->insert('table_name', $data); if($this->db->affected_rows() > 0){ return TRUE; } else{ return FALSE; } } public function update_function() { $this->db->where('id', $id); $this->db->update('test_table', $data); if($this->db->affected_rows() > 0){ return TRUE; } else{ return FALSE; } }Ajax Code in my view.
$.ajax({ type: 'POST', async: true, dataType: 'Json', url: save_experience, data: $('#candidata_exp_form').serialize(), success: function (response) { //doing something with ajax success },error: function (msg) { alert(msg.message); // I know I can give alert message here but. //I don't want to give alert message here. //I want to indicate user that the error occure whilt insert or update seperately. } });最满意答案
你必须要了解两件事。
表单验证错误和ajax错误是不同的。
Ajax错误 - 不是验证错误。 这意味着,假设您调用了一个函数,并且该函数中存在php错误,或者404错误。 那时将调用.error() 。
Ajax成功 - 没有错误(没有语法错误)。
所以现在你应该在ajax success()编写逻辑。
$.ajax({ type: 'POST', async: true, dataType: 'Json', url: save_experience, data: $('#candidata_exp_form').serialize(), success: function (response) { alert(response.message);// this will alert you the message which you have put in `json_encode()` } });You have to understand 2 things.
Form validation error and ajax error is different.
Ajax error - Is not validation error. It means, suppose you call a function and there is php error in that function, or 404 error. At that time .error() will be called.
Ajax Success - No error(No syntax error).
So now you should write the logic in ajax success().
$.ajax({ type: 'POST', async: true, dataType: 'Json', url: save_experience, data: $('#candidata_exp_form').serialize(), success: function (response) { alert(response.message);// this will alert you the message which you have put in `json_encode()` } });更多推荐
发布评论