errata
errata

Reputation: 6031

Stuck with Codeigniter and jQuery.ajax

So... thanks to one of stackoverflow users I tried to implement this fancy feature into my existing Codeigniter application...

In my View I have this:

<script type="text/javascript">
$(function() {
    $(".submit_op").click(function() {
        var dataString = $("#op_form").serialize();
        var url = "<?php echo site_url('submit/insert_data'); ?>";
        $.ajax({
            type: "POST",
            url: url+"/"+dataString,
            data: dataString,
            cache: false,
            success: function(html){
                //$("div#op").prepend(html); //PROBLEM HERE???
                $("div#op").prepend("<div>TEST</div>");
                $("div#op div:first").fadeIn("slow");
                //$("#debug").append("<font color=green><b>OK!</b></font> : " + dataString + "<br/>");
            },
            error: function(html){
                //$("#debug").append("<font color=red><b>ER!</b></font> : " + dataString + "<br/>");
            }
        });
        return false;
    });
});
</script>

<div id="debug"></div>

<?php
//here goes some data from db... newly added div should go in top of other divs
foreach ($some_data_sent_from_controller as $var) {
    echo "<div id=\"op\">";
    echo "<table width=\"100%\" border=\"0\">";
    //showing data
    echo "</table>";
    echo "</div>";
}

echo "<form action=\"#\" id=\"op_form\">";
//some clickable stuff...
echo br().form_submit('submit', 'OK', 'class="submit_op"');
echo "</form>";

In my Controller I have a function which handles data sent from View:

function insert_data($input) {
    $this->load->model('blah_model');
    //processing serialized data and sending it to corresponding tables via Model
    $this->blah_model->add_to_table($some_data);
    $this->blah_model->add_to_another_table($some_other_data);

}

And the Model is not a biggy :)

function add_to_table($data){
            //processing data...
    $insert = $this->db->insert('my_table', array('array_which_contains_actual_data'));
    if ($insert == TRUE) {
        return TRUE;
    } else {
        return FALSE;
    }
}
//etc.

As far as I can tell, my problem is not in my M-V-C pattern, since every time I submit a form the data is correctly inserted in all possible tables in my relational db... But the newly added row just won't show up unless I refresh a page.

I think that I'm doing something wrong inside of my jQuery.ajax lines... If I run my script with this line $("div#op").prepend("<div>TEST</div>"); and when I submit a form, I get desired result - text TEST shows up on top of my page every time I submit... But if I change that line to $("div#op").prepend(html); nothing show up until refreshing...

What am I doing wrong here??

Thanks a lot for any help!

Upvotes: 0

Views: 761

Answers (1)

errata
errata

Reputation: 6031

wow, this was probably pretty lame from me... But in the end I figured out that I have to echo out my result in controller, not return it... So when I change the function in my controller into

function insert_data($input) {
    $str = "<div>KILLROY WAS HERE!</div>";
    echo $str; // <----- !!!!!!
}

I can see a message on my page...

Now to other things... Thanks for self-brainstorming :)

Upvotes: 1

Related Questions