坚持使用Codeigniter和jQuery.ajax

时间:2022-10-08 07:31:29

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

所以...感谢*用户之一,我试图在我现有的Codeigniter应用程序中实现这个奇特的功能......

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:

在我的控制器中,我有一个处理从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.

据我所知,我的问题不在我的MVC模式中,因为每次我提交表单时,数据都会正确地插入到我的关系数据库中的所有可能的表中...但是新添加的行只是不会显示除非我刷新页面。

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...

我认为我在jQuery.ajax行中做错了...如果我用这行$(“div#op”)运行我的脚本.prepend(“

TEST ”);当我提交表格时,我得到了理想的结果 - 每次提交时,文本TEST都出现在我的页面顶部...但是如果我将该行更改为$(“div#op”)。prepend(html);在刷新之前没有任何表现......

What am I doing wrong here??

我在这做错了什么?

Thanks a lot for any help!

非常感谢您的帮助!

1 个解决方案

#1


1  

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 :)

现在对其他事情...感谢自我头脑风暴:)

#1


1  

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 :)

现在对其他事情...感谢自我头脑风暴:)