在codeigniter中发送Ajax错误中的自定义错误消息(Send custom error message in Ajax error in codeigniter)

编程入门 行业动态 更新时间:2024-10-14 18:20:32
在codeigniter中发送Ajax错误中的自定义错误消息(Send custom error message in Ajax error in codeigniter)

我目前正在使用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()` } });

更多推荐

本文发布于:2023-07-15 17:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1116654.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   自定义   消息   Ajax   codeigniter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!