Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 坚持使用Codeigniter和jQuery.ajax_Php_Ajax_Codeigniter_Jquery - Fatal编程技术网

Php 坚持使用Codeigniter和jQuery.ajax

Php 坚持使用Codeigniter和jQuery.ajax,php,ajax,codeigniter,jquery,Php,Ajax,Codeigniter,Jquery,所以。。。多亏了stackoverflow的一个用户,我才尝试在我现有的Codeigniter应用程序中实现这一奇特的功能 我认为: <script type="text/javascript"> $(function() { $(".submit_op").click(function() { var dataString = $("#op_form").serialize(); var url = "<?php echo site_u

所以。。。多亏了stackoverflow的一个用户,我才尝试在我现有的Codeigniter应用程序中实现这一奇特的功能

我认为:

<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>";
而且这个模型不是一个大模型:)

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

我认为我在jQuery.ajax行中做了一些错误的事情。。。如果我使用以下行运行脚本
$(“div#op”).prepend(“TEST”)当我提交表单时,我会得到想要的结果-每次我提交表单时,页面顶部都会显示文本
TEST
。。。但如果我将该行更改为
$(“div#op”).prepend(html)在刷新之前不会显示任何内容

我在这里做错了什么


非常感谢你的帮助

哇,这对我来说可能太差劲了。。。但最后我发现我必须在控制器中回显我的结果,而不是返回它。。。因此,当我将控制器中的函数更改为

function insert_data($input) {
    $str = "<div>KILLROY WAS HERE!</div>";
    echo $str; // <----- !!!!!!
}
函数插入_数据($input){
$str=“KILLROY在这里!”;
echo$str//
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.
function insert_data($input) {
    $str = "<div>KILLROY WAS HERE!</div>";
    echo $str; // <----- !!!!!!
}