CodeIgniter Forums
help with simple ajax - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: help with simple ajax (/showthread.php?tid=36102)



help with simple ajax - El Forum - 11-21-2010

[eluser]Unknown[/eluser]
Hi there, I'm new to ajax and don't know the first thing about it, but I need to use it on my site. I watched this tutorial on Nettuts+ and got a lot from it, but it still isn't enough for me to get ajax, since in my case, it doesn't work.

The main functionality of my site is recording that a person attended a lecture. I display this data in a view and the data is displayed in a table from which I want to get the person id, and the lecture id. If I click on a link inside a tablecell which represents the arrival of a person X on a lecture Y, I want AJAX to register that in the database. If I do this manually (using phpmyadmin) it works great, but I need AJAX.

I have a model called administration which has two functions. One of them (get_arrivals) is for retrieving data from a table which works great, but the other one, related to AJAX is causing me problems.

Code:
<?php

    class Administration extends Model
    {        
        
        function get_arrivals()
        {
            /* works, but too long to post */
        }

        function save_arrival($data)
        {
            
            $this->db->insert('people_lectures', $data);
        }
        
    }
    
?>

The controller I use is called arrivals
Code:
<?php

    class Arrivals extends Controller
    {
        function lectures()
        {
            $this->load->model('administration');

            if ($this->input->post('person'))
            {
                $data['person_id'] = $this->input->post('person');
                $data['lecture_id'] = $this->input->post('lecture');

                $this->administration->save_arrival($data);
            }

            $data['rows'] = $this->administration->get_arrivals();



            $data['main_content'] = 'arrivals/lectures';
            $this->load->view('templates/template', $data);
        }
    }
?>

My jQuery function to get the person_id and lecture_id is this
Code:
$(function()
{
        
        $('td a').click(function(){
        
            var student_id = $(this).parent().parent().attr("data-row-id");

               var lecture_id = $(this).parent().attr("data-col-id");
            
            $.ajax({
                url: window_location,
                type: 'POST',
                data: { person: person_id, lecture: lecture_id, arived: yes},
                success: function(msg) { location.reload(); }
            });
        });
}
);

The problem is when I run this in the browser, and click on a link inside a tablecell nothing happens (no database action, no AJAX action). I know I'm doing something wrong but I can't figure out what. Please give me a hand with this.


help with simple ajax - El Forum - 11-21-2010

[eluser]smilie[/eluser]
Hm... in your jQuery you query for success: ... but your function is not returning anything :S

Try:

Controller:
Code:
if ($this->input->post('person'))
            {
                $data['person_id'] = $this->input->post('person');
                $data['lecture_id'] = $this->input->post('lecture');

                if($this->administration->save_arrival($data))
                {
                     return TRUE;
                }
            }

And in model:
Code:
function save_arrival($data)
        {  
            if($this->db->insert('people_lectures', $data))
            {
                return TRUE;
            }
        }

I would also recommend that you also catch errors in live script Smile

Cheers,
Smilie


help with simple ajax - El Forum - 11-21-2010

[eluser]CroNiX[/eluser]
I believe
Code:
data: { person: person_id, lecture: lecture_id, arived: yes},
should be
Code:
data: { person: student_id, lecture: lecture_id, arived: "yes"},

You never defined a "person_id", but you did define "student_id" but you never used it.
"yes" is a string and should be in quotes.

Nothing to do with the functionality of the code, but "arived" is spelled "arrived".

Also, where is "window_location" that you are using for the ajax call defined in your javascript?