简体   繁体   中英

Unable to update database with ajax in codeigniter

I have passed an array tree from controller to view and I am using a helper for recurssion to display it in the form of unordered lists. I have a button with each list item to move one step up. My view div is like this:

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

here, display_tree is a helper:

<?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;
}
?>`

controller function:

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, $parent, $group, $ordering);
 }

n model is:

function up($s_p, $parent, $group, $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, $service, $parent, $group, $ordering) {
    $var = 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('service =', $service);
    $this->db->set($var);
    $this->db->update($this->_table_name);  
}

Now I am trying to update the ordering column of database table with ajax. Ajax code is:

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

But when i click on up button, nothing is happening. Please help me.

There is an problem in your AJAX request. You forgot to add & seprator between each parameters of the data you want to send :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM