如何在控制器代码点火器中从 ajax 获取数据


How to get the data from ajax in controller codeigniter

我的视图中有一个可编辑的表格。起初,表中没有数据,但用户可以在表中添加数据,因为它是可编辑的。而且表中没有确切的行数,因为我还有一个可以添加新行的按钮。我想获取用户添加的数据并将其保存在数据库中。

我有这个代码:

视图:

 <table class="table " id="memberTB">
    <thead><tr><th >First Name</th><th >Middle Name</th><th>Last Name</th></tr></thead>
    <tbody>
                   <tr id="first"><td><span class="edit"></span></td>
                   <td><span class="edit"></span></td>
                   <td><span class="edit"></span></td></tr>
    </tbody>
     <button type="button" class="btn btn-link" id="addrow"><span class="fa fa-plus"> Add new row</span></button>
</table>
<br><button type="button" class="btn" id="savebtn">Save</button> <a href="#" class="btn" id="resetbtn">Reset</a>

.JS:

$.fn.editable.defaults.mode = 'inline';
$.fn.editable.defaults.showbuttons = false;
$.fn.editable.defaults.url = '/post';
$.fn.editable.defaults.type = 'text';
// make all items having class 'edit' editable
$('.edit').editable();
    // this is to automatically make the next item in the table editable
$('.edit').on('save', function(e, params){
    var that = this;
    // persist the old value in the element to be restored when clicking reset
    var oldItemValue = $(that)[0].innerHTML;
    if (!$(that).attr('oldValue')) {
        console.log('persisting original value: ' + oldItemValue)
        $(that).attr('oldValue', oldItemValue);
    }
    setTimeout(function() {
        // first search the row
        var item = $(that).closest('td').next().find('.edit');
        console.log(item);
        if (item.length == 0) {
            // check the next row
            item = $(that).closest('tr').next().find('.edit');
        }
        item.editable('show');
    }, 200);
});
$('#resetbtn').click(function() {
    $('.edit').each(function() {
        var o = $(this);
        o.editable('setValue', o.attr('oldValue'))  //clear values
        .editable('option', 'pk', o.attr('pk')) //clear pk
        .removeClass('editable-unsaved')
        .removeAttr('oldValue');
    });
});

$('#savebtn').click(function() {
    var person = [];
    var x=1;
    $('tbody tr',$('#memberTB')).each(function(){
        for(var i = 0 ; i < cells ; i++)
        {
            person[x][i]=$(this).find('td').eq(i).text();
        }
        x++;
    });
    $.ajax({
        url: '<?php echo base_url("index.php/test/Savedata");?>',
        type: "post",
        data: { values: arraylng },
        cache: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
        }
    });
});
$('#addrow').click(function() {
    $('#memberTB > tbody:last').append(' <tr><td><span class="edit"></span></td><td><span class="edit"></span></td><td><span class="edit"></span></td></tr>');
    $('.edit').editable();
});

控制器:[测试内部.php]

public function saveData(){
  $this->load->model('test_model');
  $myArray = $_REQUEST['values'];
  echo sizeof($myArray);
}

每当我单击保存按钮时,根本没有任何响应。我哪里做错了?请帮助我..

新增信息: 我没有在这里包含我的 SQL 插入语句,因为我想先测试$myArray中是否有数据,以及我是否在表中添加了数据。

最好使用这个 ajax

var arraylng = [3,4,7];
$.ajax({
    url: '<?php echo base_url("index.php/test/Savedata");?>',
    type: "post",
    data: {values: JSON.stringify(arraylng)},
    cache: false,
    success: function (response) {
        alert(response);
    }
});

arraylng 是一个数组,代码中不存在。我将其添加到此处进行调试。假设你想发送 person[] 数组,你写,数据:{值:JSON.stringify(person)}。现在,人员数组可能不存在,因为 for 中的"i <单元格"。什么是细胞?>

响应这个词只是一个名字,任何名字,但最好避免使用"数据"。

在test.php中,什么是sizeof($myArray)?只是,回声$myArray;

单击"保存"时,必须在警报中获取$myArray内容。