Getting undefined variables and calls to undefined functions.

#1
[eluser]Unknown[/eluser]
Hi! I'm a beginner at using CI so please bear with me. I came across a problem in a project I am doing. I've tried searching but never found similar topics. I hope someone can give me a quick answer to my questions.

I am trying to load some data into a view and inject the resulting html + data into my main view via AJAX. What's happening is that when I try to view this injected html, I get a bunch of PHP errors, all undefined variables and calls to undefined CI functions.

I have included stripped down versions (just the related parts) of my code here:

Here is my controller:

Code:
<?php

class My_loader extends Controller {

    function My_loader()
    {
        parent::Controller();
    }
    
    function get_wrkload() {
        $id = $this->input->post('wid');
        $this->load->model('e_log_model');
        $data['record'] = $this->e_log_model->getWorkLoad($id);
        $this->load->view('record',$data);
    }
?>

My model:
Code:
<?php

class E_log_model extends Model {

    function E_log_model(){
        parent::Model();
        $this->load->database();
        $this->load->library('encrypt');
    }
        function getWorkLoad($id) {
        $query = $this->db->get('tbl_workload');
        $this->db->where('id', $id);
        if ($query->result() < 0) {
            return false;
        } else {
            return $query->result();
        }
    }

The jQuery AJAX:
Code:
$('.edit').click(function() {
            wid = $(this).attr("value");
            $.ajax({
                type: "POST",
                url: "&lt;?php echo site_url("my_loader/get_wrkload");?&gt;",
                data: "wid="+wid,
                success: function(data,msg){
                    $('#result-area').load("&lt;?php echo base_url(); ?&gt;system/application/views/record.php");
                }
            });
            $('#edit-dialog').dialog('open');    
        });

In my view I have a div id'ed #result-area where the data is supposed to load. The record.php view is simply a fragment that wil be injected into the #result-area container. It's kinda long to post the whole code so here's part of it:
Code:
&lt;?php print_r($cat_list); ?&gt;
        <table class="add-wrk-tbl ui-widget">
            <tbody>
                <tr>
                    <td class="wl-label ui-widget-content ui-corner-all">
                        Priority&lt;?php echo $record['priority'];?&gt;
                    </td>
                    <td class="wl-field ui-widget-content ui-corner-all">
                        &lt;?php
                            $att0 = 'id="e_priority" class="{validate:{required:true}} ui-widget-content ui-corner-all"';
                            echo form_dropdown('e_priority', $priority_list, $record['wrk_priority'], $att0);
                        ?&gt;
                    </td>
..... and so on

I assure you it is a properly constructed table. I've used the same in some other part of my project and it works well.

I do realize that the problem is that data I passed to the record view through my controller doesn't seem to exist, and the CI functions I used here are being ignored.

What could probably cause this? Is there a better way to achieve the same result?

#2
[eluser]chefsalat[/eluser]
it looks like your controller is returning the complete view, so there is no need to call it again in your ajax success function.
try something like this:
success: function(data) {
$('#result-area').html(data);
}

#3
[eluser]Unknown[/eluser]
[quote author="chefsalat" date="1265121138"]it looks like your controller is returning the complete view, so there is no need to call it again in your ajax success function.
try something like this:
success: function(data) {
$('#result-area').html(data);
}[/quote]

Thanks for a quick response. I guess you were right. I tried that and the undefined CI function errors disappeared. However, I got undefined index errors this time. It must be the data that was exploded into the view. I was referencing it improperly. Anyway, I think I can sort this one out myself now. Smile Thank you for clarifying my issue. New to jQuery too so your response also taught me a few new things about it. Will post again if I still encounter some more.

#4
[eluser]chefsalat[/eluser]
Glad I could help! Started learning both jQuery and Codeigniter only a month ago as well, but once you get the principles, it goes fast.
As for your second problem, your treating the record in your view as array, but when you pass it from the model with query->result() it is passed as an object. So you have to address it as:

$record->priority;

If you prefer to work with arrays, you can pass them out with query->result_array(), but I suggest to stick with objects, as it's the more 'natural' CI way.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.