CodeIgniter Forums

Full Version: AJAX troubleshoot if anyone is bored. :-)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]stevefink[/eluser]
This isn't a CI issue. It's a user error. :-)

Anyhow. I have a URL that looks like this:

http://f1auto/console/addvehicle/photos/1

This URL is mangled with URI routing, so if clarification is needed after this post, I'll be sure to respond. I have a DIV within the main view that looks like this:

Code:
<div id="showPhotos">
                            
                                $('#showPhotos').load('get_photos', {vehicle_id: &lt;?=$this->uri->segment(4);?&gt;});
                            
                        </div>

get_photos resembles the following:

Code:
function get_photos()
    {
        $id = $this->input->post('vehicle_id');
        // sanitize it just in case
        $vehicle_id = $this->input->xss_clean($id);
        // get all photo filesystem locations.
        $photos = $this->autodb->get_photos($vehicle_id);
        //log_message('debug', "vehicle_id: " . print_r($vehicle_id, TRUE));
        //log_message('debug', "photos " . print_r($photos, TRUE));
        return $photos;
    }

and finally get_photos in the model resembles the following:

Code:
function get_photos($vehicle_id)
    {
        // return all available photos for vehicle
        $photos = $this->db->getwhere('photos', array('vehicle_id'=>$vehicle_id));
        return $photos;
    }

I'm trying to load a separate view depending on the 4th URI segment which is the $vehicle_id. For the sake of brevity and debug, the .load() is calling:

Code:
&lt;?php foreach($photos->result() as $photo): ?&gt;
    <br><br> <hr>
    
    &lt;!-- img src="&lt;?= //base_url() ?&gt;uploads/&lt;?= //$photo->photo_id ?&gt;_thumb.jpg" -- >
    <p> photo_id:         &lt;?=$photo->photo_id;?&gt;        </p>
    <p> vehicle_id:     &lt;?=$photo->vehicle_id;?&gt;    </p>
    <p> photoloc:         &lt;?=$photo->photoloc;?&gt;        </p>
    <p> caption:         &lt;?=$photo->caption;?&gt;        </p>
    
&lt;? endforeach; ?&gt;

However after debugging with Firebug, I seem to be getting zero data back from the server.

Can anyone be kind enough to help me recover my fumble?

Thanks!

El Forum

[eluser]coolfactor[/eluser]
I don't see where you're loading that view. Your get_photos() method is returning the $photos array directly.

El Forum

[eluser]stevefink[/eluser]
Eeep, just noticed get_photos() was out of date in my controller. The view gets loaded here:

Code:
function get_photos()
    {
        $id = $this->input->post('vehicle_id');
        // sanitize it just in case
        $vehicle_id = $this->input->xss_clean($id);
        // get all photo filesystem locations.
        $photos = $this->autodb->get_photos($vehicle_id);
        //log_message('debug', "vehicle_id: " . print_r($vehicle_id, TRUE));
        //log_message('debug', "photos " . print_r($photos, TRUE));
        $this->load->view('console/vehicle_photos_view', $photos);
    }

El Forum

[eluser]stevefink[/eluser]
What if I just eliminate AJAX from the equation to make this a tad simpler.

I revised my function to look like this in the controller:

Code:
function index()
    {
        if($this->uri->segment(4) === FALSE)
        {
            redirect('', 'refresh');
        }
        
        $id = $this->input->post('vehicle_id');
        // sanitize it just in case
        $vehicle_id = $this->input->xss_clean($id);
        // get all photo filesystem locations.
        $data['photos'] = $this->autodb->get_photos($vehicle_id);
                log_message('debug', print_r($data, TRUE));
        $this->load->view('console/photos_view',$data);
    }

And my photos_view contains the following:

Code:
<div id="showPhotos">
                            &lt;?php foreach($photos->result() as $photo): ?&gt;
                                <hr />
                            
                                <p> photo_id:         &lt;?=$photo->photo_id;?&gt;        </p>
                                <p> vehicle_id:     &lt;?=$photo->vehicle_id;?&gt;    </p>
                                <p> photoloc:         &lt;?=$photo->photoloc;?&gt;        </p>
                                <p> caption:         &lt;?=$photo->caption;?&gt;        </p>
    
                            &lt;? endforeach; ?&gt;
                        </div>

Still nothing showing up. log_message DEBUGGING shows the following dump of $data:

Code:
DEBUG - 2007-08-27 19:36:31 --&gt; Array
(
    [photos] => CI_DB_mysql_result Object
        (
            [conn_id] => Resource id #39
            [result_id] => Resource id #57
            [result_array] => Array
                (
                )

            [result_object] => Array
                (
                )

            [current_row] => 0
            [num_rows] => 0
        )

)

El Forum

[eluser]FrankieShakes[/eluser]
Stupid question... but what does your DB table look like? Can you show a sample row of the data you have in it?

El Forum

[eluser]Luipaard[/eluser]
I could be wrong, but I believe you can't put javascript in your HTML just like that. It has to included from a JS file, or embedded within script tags?

EDIT:

Nevermind, the forum is filtering the script tag.

El Forum

[eluser]obiron2[/eluser]
Sorry, I can't see where you are calling a javascript function which would then run the Ajax callback.

The ajax needs to pass the full URI to CI and then have a destination JS function to process the result (normally replace innerHTML of a DOM object with the responseText)

The beauty of using CI with Ajax is you can test the URI call independently of the Ajax just by keying the URI in the the resource bar.

Are you wrapping the Ajax call in an object? If you are creating and only using one ajax object and you can have several ajax calls going at once, the original call can get overwritten.

use this http://www.hunlock.com/blogs/The_Ultimate_Ajax_Object to create the ajax object and each call can then have a separate object. This has saved me no end of grief. I have read somewhere that some browsers will restrict you to two syncronous XMLHttp requests.

obiron

PS use Firefox with Firebug. You can see exactly what is going on with the XMLHttp requests

El Forum

[eluser]jkevinburton[/eluser]
Just to clarify that statement. You can call a PHP file in javascript (ajax method) and have it return javascript.. as long as you make the header text/javascript.

[quote author="Luipaard" date="1188295158"]I could be wrong, but I believe you can't put javascript in your HTML just like that. It has to included from a JS file, or embedded within script tags?
[/quote]