Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5944809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:35:32+00:00 2026-05-22T16:35:32+00:00

I post data via ajax to my controller which does some validating and sends

  • 0

I post data via ajax to my controller which does some validating and sends it off to the model. My table has a unique index to prevent duplicates. Unique inserts work fine, my problem is I can’t seem to catch the duplicate error and deal with it.

Model Code:
function insert($data) {

    //die($this->db->last_query);

    try{
        $query = $this->db->insert('subscriber',$data);
    } catch (Exception $e) {
         echo 'Caught exception: ',  $e->getMessage(), "\n";
    }



    if($query){
        return true;
    } else {
        return false;
    }

}

jQuery ajax call:
function runAjax() {

$('#theform').append('<p id="loading"><img src="<?php echo base_url();?>' + 'images/loader.gif"/>');

$.ajax({
   type: "POST",
   cache: false,
   url: "<?php echo base_url();?>" + 'contact/mail',
   data: 'fname=' + $('#fname').val() + '&lname=' + $('#lname').val() + '&email=' + $('#email').val(),
   success: function(msg){

   $('#response').remove();
   $('#theform').append('<p id="response">' + msg + '</p>');
   $('#loading').fadeOut(500, function() {
        $(this).remove();
    });
   },
   error: function(msg){
   $('#theform').append('<p id="response">' + msg + '</p>');
   }

});

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:35:33+00:00Added an answer on May 22, 2026 at 4:35 pm

    Errors from MySQL are not thrown as exceptions and hence you cannot catch them.

    Instead, if you query results null, you can print out the messages with db->_error_message() and db->_error_number() (http://codeigniter.com/forums/viewthread/79950/#413830). As such, you could rewrite your code as follows:

        $query = $this->db->insert('subscriber',$data);
    
        if($query){
            return true;
        } else {
            $msg = $this->db->_error_message();
            $num = $this->db->_error_number();
            // Do something with msg and num
            return false;
        }
    

    Also note that error inside $.ajax is only called if there is an error performing the request. Even though your database query fails, it still doesn’t make the Ajax query fail. Hence, you will never the Ajax error.

    I see two options here:

    1) Make the HTTP request fail somehow (e.g. return HTTP 404 error).

    2) Process your MySQL errors inside success method, instead of error:

    success: function(msg){
    
       if ( msg == 'yoursuccessmessage' ) {
          // Handle success
       } else {
          // Handle error
       }
    },
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to send some data via JQuery ajax to a grails controller Here's
The Setting: The program in question tries to post form data via an AJAX
I want to post some data via javascript to another domain. Something like: http://www.othersite.com/submitfunnyname?name=blah
I have a page where the user sends data (via ajax) to a php
I have a form, in which I need to send data selected via AJAX
I have this javascript that pulls some data via an ajax request the method
I am attempting to send some data via JSON to a MVC controller action:
I try to post some data via json objects to my asp.net mvc view,
When sending data via POST or GET with jQuery you use for format {
CodeIgniter allows access to POSTed data via: $this->input->post('input_name'); where 'input_name' is the name of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.