【问题标题】:Stuck with Codeigniter and jQuery.ajax卡在 Codeigniter 和 jQuery.ajax
【发布时间】:2011-03-05 13:43:32
【问题描述】:

所以...感谢其中一位 stackoverflow 用户,我尝试在我现有的 Codeigniter 应用程序中实现 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>";

在我的控制器中,我有一个处理从视图发送的数据的函数:

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

}

而且模型不是很大:)

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.

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

我认为我在我的 jQuery.ajax 行中做错了什么...如果我使用这一行 $("div#op").prepend("&lt;div&gt;TEST&lt;/div&gt;"); 运行我的脚本,并且当我提交表单时,我会得到想要的结果 - 文本 TEST 显示每次提交时都会显示在我的页面顶部...但是如果我将该行更改为 $("div#op").prepend(html); 在刷新之前什么都不会显示...

我在这里做错了什么??

非常感谢您的帮助!

【问题讨论】:

    标签: php ajax codeigniter jquery


    【解决方案1】:

    哇,这对我来说可能很蹩脚......但最后我发现我必须在控制器中回显我的结果,而不是返回它......所以当我将控制器中的函数更改为

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

    我可以在我的页面上看到一条消息...

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      • 2011-11-08
      相关资源
      最近更新 更多