【问题标题】:How to get the data from ajax in controller codeigniter如何从控制器codeigniter中的ajax获取数据
【发布时间】:2015-03-14 04:10:49
【问题描述】:

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

我有这个代码:

查看:

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

控制器:[在test.php中]

public function saveData(){

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

每当我点击保存按钮时,根本没有任何反应。我哪里出错了?请帮帮我..

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

【问题讨论】:

  • 在 JS 中有 //ajax emulation 后跟 $.mockjax({ 的原因吗?
  • 我已经删除了。实际上,我只是在参考中复制了它。而且当我尝试删除时,它不会影响表格..所以现在,我不会将它包含在js中..@NathanTuggy
  • 您检查控制台是否有错误?
  • @siddhesh..不,我没有检查过,我实际上不知道如何..我正在使用 aptana..
  • 测试时(推荐谷歌浏览器)按 F12 进入开发者面板,进入控制台标签并检查 JavaScript 错误。也可以在其他浏览器中完成,但您需要找到方法。

标签: javascript php jquery ajax codeigniter


【解决方案1】:

最好使用这个 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[] 数组,你写的是 data: {values: JSON.stringify(person)}。 现在,由于 for 中的“i

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

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

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

【讨论】:

  • 感谢您的回答。是的,我在警报中得到了 $myArray 内容。但它怎么能转到其他页面呢?我的意思是,不仅仅是一个警报,它真的会加载 test.php 吗?我该怎么做?
  • 你得到了警报,因为实际上 test.php 已经得到了数据,并将其发送回视图。在 saveData() 中使用 $myArray 做一些事情并将其发回。你会得到不同的回应。这就是ajax 所做的。你期待什么?而且你不需要只是 alert() 它。你可以在你的成功函数中操作它。
  • 啊..好的。但是如果我在 test.php 中有一个插入函数呢?
  • 可以在success函数中添加sql insert语句吗?
  • 没有。您在 html 中以字符串形式在 saveData() 中收集了数据。 $myArray 是一个字符串。您所要做的就是在 saveData 中构建正确的数组结构,然后将其保存。您可以使用explode() 从字符串中创建一个数组。成功功能“放置”在视图中。在这个函数中,您将编写您想要在 html 页面上显示的内容。
猜你喜欢
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-11
  • 2015-01-03
  • 1970-01-01
相关资源
最近更新 更多