CodeIgniter Forums
Ajax newbie - nothing happening - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Ajax newbie - nothing happening (/thread-29608.html)



Ajax newbie - nothing happening - El Forum - 04-15-2010

[eluser]richzilla[/eluser]
Hi All,

Ive never really looked at ajax much before, JS is not my strong point, and im having a bit of trouble with this function:
Code:
var user_data =
                                             {
                                                userid: 4
                                             };
                                         $.ajax({
                                            url: "<?php echo site_url('user/users/ajax'); ?>",
                                            type: "POST",
                                            data: user_data,
                                            success: function(retdata)
                                            {
                                                 $('input[name=customer_name]').val(retdata.name);
                                                 $('textarea[name=customer_address]').text(retdata.address);
                                                 $('input[name=customer_email]').val(retdata.email);
                                                 $('input[name=customer_tel]').val(retdata.tel);
                                                 $('input[name=user_id]').val(item.id);
                                            }


And the method in my code:
Code:
function ajax()
        {
            $user_id = $this->input->post('userid');
            $user = $this->user_model->get_user($user_id);

            $user_data = "[{name:\"".$user->sub_user_name."\",";
            $user_data .= "address:\"".$user->sub_user_address."\",";
            $user_data .= "email:\"".$user->user_email."\",";
            $user_data .= "tel:\"".$user->sub_user_tel."\"}]";

            return $user_data;
        }


Nothing happens when i call it with javascript, and im not entirely sure where to start looking.
The function works when i call it normally (without using JS).

Whats the best format to return the data from the server in?

any help would be appreciated.


Ajax newbie - nothing happening - El Forum - 04-15-2010

[eluser]philm[/eluser]
I find it really useful to use Firefox with a couple of addons to trouble shoot these types of things.

https://addons.mozilla.org/en-US/firefox/addon/60
With this one, once installed you'll have an extra toolbar with all kinds of cool toys in it. On the far right there are 3 little icons which flag errors in HTML, CSS and JAVASCRIPT. Click on the little JS icon and it will open a popup and show you the error, nice and easy Smile

http://getfirebug.com/
Here you can track and trace all ajax calls under the 'Console' tab.

Hope this helps...


Ajax newbie - nothing happening - El Forum - 04-15-2010

[eluser]nelson.wells[/eluser]
Without looking too much at it, your php function should be echoing the data, not returning it. What I like to do is put data into an associative array, json_encode it, and then 'return' it to the calling js function and output like you are doing in js.

Code:
$data = array(
    'name' => $user->sub_user_name,
    'address' => $user->sub_user_address,
    'email' => $user->sub_user_email,
    'tel' => $user->sub_user_tel
);

$json = json_encode($data);

echo $json;



Ajax newbie - nothing happening - El Forum - 04-15-2010

[eluser]danmontgomery[/eluser]
[quote author="philm" date="1271348488"]I find it really useful to use Firefox with a couple of addons to trouble shoot these types of things.

https://addons.mozilla.org/en-US/firefox/addon/60
With this one, once installed you'll have an extra toolbar with all kinds of cool toys in it. On the far right there are 3 little icons which flag errors in HTML, CSS and JAVASCRIPT. Click on the little JS icon and it will open a popup and show you the error, nice and easy Smile

http://getfirebug.com/
Here you can track and trace all ajax calls under the 'Console' tab.

Hope this helps...[/quote]

Seconding this post... these two addons are essential and everyone should have them.