微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 无法在codeigniter中使用ajax更新数据库

我已经从控制器传递了一个数组树到视图,我正在使用帮助程序进行递归,以无序列表的形式显示它.我有一个按钮,每个列表项都向上移动一步.我的观点div是这样的:

<div id="div">
<?PHP 
   $ordering = count($trees[$grp->id]);                             
?>     
<a href="javascript:Swapit('swapper-first','swap')" onClick="showIFrame('<?PHP echo site_url('service_group_services/edit/0_' . $ordering . '_' . $grp->id); ?>');">
    <button type="button" class="btn btn-default btn-xs btn-label-left">
        <i class="fa fa-plus"></i>
    </button>
</a>                                      
<?PHP                                                   
    display_tree($trees[$grp->id],$grp->id);                                                            
?> 
</div>

<?PHP endforeach; ?>

这里,display_tree是一个帮手:

<?PHP function display_tree($array,$grp) {

    foreach ($array as $value): {
        $ordering = 0;
        if (isset($value->childs[0])) {
            $val = $value->childs;
            $ordering = count($val);          
        }

        echo "\n<ul>";
        echo "\n<li>";

        if($value->type != 'SERVICE') {
            echo '<a href="javascript:Swapit('."'" .'swapper-first'."'" .','."'" .'swap'."'" .')" onClick="showIFrame('."'".'service_group_services/edit/'.$value->service.'_' . $ordering . '_'.$grp ."'" .')"><span> <i class="fa  fa-plus"></i></span></a>';
        }
        if($value->ordering != 0) {
            echo  '<a href="#" onclick="load_data_ajax('.$value->service_parent. ',' . $value->ordering . ',' . $value->service_group . ',' . $value->service . ')"><span> <i class="fa  fa-sort-up"></i></span></a>';
        }
        echo '<a href="service_group_services/delete/'.$value->service_parent. '_' . $value->service . '_' . $value->service_group . '_'. $value->ordering .'"><span> <i class="fa fa-times"></i></span></a>'. $value->name .'</li>';

        if (isset($value->childs[0])){
             $val = $value->childs;
            display_tree($val,$grp);
        }
        echo '</ul>';
    }
    endforeach;
}
?>`

控制器功能

function move_up(){
     $parent = $this->input->post('service_parent');
     $ordering = $this->input->post('ordering');
     $group = $this->input->post('service_group');
     $service = $this->input->post('service');

     $s_p = $this->session->userdata('service_provider');
     $this->Mdl_service_group_services->up($s_p,$parent,$group,$ordering);
     $this->Mdl_service_group_services->up1($s_p,$service,$ordering);
 }

n模型是:

function up($s_p,$ordering) {
    $data = array(
        'ordering' => $ordering   
    );
    $this->db->where('service_provider =',$s_p);
    $this->db->where('service_group =',$group);
    $this->db->where('service_parent =',$parent);
    $this->db->where('ordering =',--$ordering);
    $this->db->set($data);
    $this->db->update($this->_table_name);
}

function up1($s_p,$ordering) {
    $var = array(
        'ordering' => --$ordering
    );
    $this->db->where('service_provider =',$parent);
    $this->db->where('service =',$service);
    $this->db->set($var);
    $this->db->update($this->_table_name);  
}

现在我试图用ajax更新数据库表的排序列.
Ajax代码是:

var controller = 'service_group_services';
var base_url = '<?PHP echo site_url(); //you have to load the "url_helper" to use this function ?>';

function load_data_ajax(parent,ordering,group,service){
    $.ajax({
        'url' : base_url + controller + '/move_up','type' : 'POST',//the way you want to send data to your URL
        'data' : 'service_parent='+parent+'ordering='+ordering+'service_group='+group+'service='+service,'success' : function(data){ //probably this request will return anything,it'll be put in var "data"
        var div = $('#div'); //jquery selector (get element by id)
            if(data){
                div.html(data);
            }
        }
    });
}

但是当我点击向上按钮时,什么也没发生.请帮我.

解决方法

您的AJAX请求中存在问题.您忘记在要发送的数据的每个参数之间添加& seprator:

function load_data_ajax(parent,service){
    $.ajax({
        ...
        'data' : 'service_parent='+parent+'&ordering='+ordering+'&service_group='+group+'&service='+service,...
    });
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐