Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Junior Member
    Join Date
    Jan 2016
    Posts
    13
    Member #
    52917

    Having problem inserting data using ajax in codeigniter

    I am new to ajax and trying to use it to insert data in my codeigniter project. But I am not able to do so. I have searched for many tutorials and tried to do the same but still it is not working.
    Here is what I have tried.
    HTML Code:
    <script>
    $('#submit').click(function(){
    var form_data={
    emp_name: $('#emp_name').val(),
    emp_dob: $('#emp_dob').val()
    };
    $.ajax({
    url: "<?php echo site_url('employee/store'); ?>",
    		type: "POST",
    		data: form_data,
    		success: function(msg){
    			alert("msg");
    		}
    	});
    	return true;
    });
    </script>
    Controller:
    PHP Code:
    function store(){
    $data=array(
    'emp_name'=>$this->input->post('emp_name'),
    'emp_dob'=>$this->input->post('emp_dob')
    );
    $this->load->model('Mdl_employee');
    $this->Mdl_employee->addEmployee($data); 
    Model:
    PHP Code:
    function addEmployee($data){
    $qry $this->db->insert('employees',$data);

    Last edited by leo037; Feb 02nd, 2016 at 04:07 AM.

  2.  

  3. #2
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Define "not working". What is it not doing that it should?

    Just a couple of things I see:
    1 - The alert in your success function is only going to alert 'msg', as you have passed it as a string.
    2 - You aren't returning anything from your controller script. You'll do that by echoing something, even if it's the string 'success'. The controller must return something in order for the AJAX script to be aware that it's complete.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  4. #3
    Junior Member
    Join Date
    Jan 2016
    Posts
    13
    Member #
    52917
    My main problem is that data is not being inserted into database.

  5. #4
    Junior Member
    Join Date
    Jan 2016
    Posts
    13
    Member #
    52917
    Finally after few try, I was able to insert into database. Now, I am looking into getting confirmation message and stopping same data from getting inserted every time click button is clicked.

  6. #5
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    You'll need to run $this->db->update if the record exists, and $this->db->insert if it doesn't. To do that, you'll need to find a way to differentiate between the 2. The CI project I worked before used IDs. If the ID was 0, that meant it was a new record. If it was greater than 0, it would update.

    For the confirmation, as I mentioned before, your PHP script will need to echo something back. So, if the insert/update is successful, you could echo "success" and echo "fail" if the insert/update fails. The echo will be passed to your success function as the data argument. From there, you can run your function to provide confirmation.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  7. #6
    Junior Member
    Join Date
    Jan 2016
    Posts
    13
    Member #
    52917
    Thanks for suggestion. I was able to get confirmation message. For now I have used two forms for insert and update. Maybe in future, I will try to do it single form. I have not been getting time to work on my project lately.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search tags for this page

insert using ajax in codeigniter

Click on a term to search for related topics.

Tags for this Thread

All times are GMT -6. The time now is 09:33 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com