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

  • Home
  • SEARCH
  • 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 3627106
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:50:46+00:00 2026-05-18T23:50:46+00:00

I am learning AJAX and have borrowed a script from NETTUTS Codeigniter from Scratch:

  • 0

I am learning AJAX and have borrowed a script from NETTUTS Codeigniter from Scratch: AJAX
The script itself works perfectly as adapted to my form, however it is not updating the database per the controller. Firebug says the post went fine from the AJAX script. I guess my question is, how and where am I failing to start the controller?

The form_open script

echo form_open('contacts/entry', $form);

Ajax script(there is validation script above what I am showing that works fine)

 var form_data = {
    fname: $('#fname').val(),
    lname: $('#lname').val(),
    email: $('#email').val(),
    phone: $('#phone').val(),
    relate: $('#relate').val(),
    ajax: '1'
};

$.ajax({
    url: "<?php echo site_url('contacts/entry'); ?>",
    type: 'POST',
    data: 'form_data',
    success: function() {
        $('#status').text('Update successful!'); 
    }
});

return false;   

});

Note that I have included “AJAX: 1” in the form_data
The controller

function entry()
{
   if ($this->input->post('ajax')) {  
   $data = array
           (
           'fname' => $this->input->post('fname'),
           'lname' => $this->input->post('lname'),
           'email' => $this->input->post('email'),
           'phone' => $this->input->post('phone'),
           'relate' => $this->input->post('relate'),
           );

   //removed validation set rules to shorten the question

   if ($this->form_validation->run() == TRUE)
   {
    $this->db->insert('contacts', $data);   
    $this->index();    
   } else
   {
    $this->index();
   }       
} 
}

Is the use of “if ($this->input->post(‘ajax’))” the right way to initiate the controller? Without the jquery the form works fine by itself and does run the controller. So I know the two individual pieces work, they just are not meshing. Thanks

  • 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-18T23:50:47+00:00Added an answer on May 18, 2026 at 11:50 pm

    First of all I’ll delete the var form_data { ajax: '1' } and use something like this:

    if($this->input->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest')
    {
        // This is AJAX
    }
    else {
        // This is standard HTTP
    }
    

    A nice way to handle this if is to use a MY_Controller. If you use it, add a function like this:

    protected function is_ajax() {
        return ($this->input->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest') ? TRUE : FALSE;
    }
    

    And you can use:

    if($this->is_ajax())
    {
        // This is AJAX
    }
    else {
        // This is HTTP
    }
    

    Then you’ve to echo the results to pass it to the actual view and handle the operation. Like this:

    if ($this->form_validation->run() == TRUE)
    {
        // Tip: you've to do the data management on MODELS
        $action = $this->db->insert('contacts', $data);
    
        // With this method you can handle AJAX and HTTP on the same
        // validation. If is AJAX you print, if not you redirect.
        if($this->is_ajax())
            echo $action;
        else {
            // if worked, redirect the user to the success 
            // else send it to the form error
            if($action)
                redirect("form_success");
            else
                redirect("form_error");
        }
    else {
        if($this->is_ajax())
            echo false;
        else {
            $this->load->view("error_form");
        }
    }
    

    And you’ve to correct you jQuery code to handle the operation:

    $.ajax({
        url: "<?php echo site_url('contacts/entry'); ?>",
        type: 'POST',
        data: form_data, // Like said jondavidjohn
        success: function(data) {
            if(data)
            {
                $('#status').text('Update successful!');
            } else {
                $('#status').text('Operation failed!');
            } 
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Ajax at the moment. The code below basically receives the echo from
Hi I am still learning Jquery and have a problem accessing form elements. My
Have run into jquery question while learning ajax. $('div_hello'); --> answers an 'Object' document.getElementById('div_hello');
I am learning ASP.net Ajax. I want to pull data from a db table
as i said in my title am learning about Ajax and i have a
I want to start learning HTML and AJAX using a Linux distribution. Can anyone
Learning from my last question , most member names seem to get included in
I am learning AJAX in zend framework step by step. I use this question
After learning about jQuery and the whole AJAX thing, I got very excited. I
I have a few .NET developers who only want to use the MS AJAX

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.