douzhao9608 2016-09-28 13:02
浏览 52
已采纳

使用codeigniter和jquery插入后如何获取最后插入的id

Please help me on how to get the last inserted id from database using codeigniter and jquery. What I want is to get the the last inserted id after inserting. I am using jquery to insert the data. I don't have any idea on how to it. Thanks a lot in advance.

SCRIPT

$('#add-tag').click(function () {
    var lang = $('#lang').val();
    var tag  = $('#tag').val();

    var data = {
        tag: tag,
        lang: lang
    }

    if (tag === '') {
        $('.error').html('<h4><i class="glyphicon glyphicon-info-sign">' +
            '</i> Field cannot be empty!</h4>').addClass('bounceIn').show();

        $('.error').delay(3000).fadeOut();
    } else {
        $.ajax({
            url: "<?= site_url('blog/add_tag'); ?>",
            type: 'POST',
            data: data,
            success: function (result) {
                //display message if successful
                $('.message').html('<h4><i class="glyphicon glyphicon-ok">' +
                    '</i> Tag has been added</h4>').addClass('bounceIn').show();
                $('.message').delay(3000).fadeOut();
                $('#tag').val('');

                $('#tags').append('<a class="tags animated fadeInDown">' +
                    '<span class="remove-tag" id="remove-tag' + lid + '">' +
                    '</span></span> ' + tag + '</a>');

                window.setTimeout(function () {
                    location.reload();
                }, 2000);
            }
        });
    }
});

VIEW

<div class="row">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div id="add-tag-form" class="display-none relative">
            <div class="well well-sm">
                <div class="row">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <h5>Add Tag</h5>
                        <input type="text" id="tagLastID" class="form-control" placeholder="Tag Last ID" readonly>
                        <input type="hidden" id="lang" class="form-control" placeholder="Lang" required value="<?= $lang; ?>">
                        <input type="text" id="tag" class="form-control" placeholder="Tag" required>
                        <br />
                        <button id="add-tag" class="btn col-md-12 col-sm-12 col-xs-12">Add Tag</button>
                        <div class="text-center"><a id="close-tag">cancel</a></div>
                    </div>
                </div>
            </div>
        </div>
        <button id="add-tag-btn" class="btn col-md-12 col-sm-12 col-xs-12">Add Tag</button>

    </div>
</div>

CONTROLLER

public function add_tag() {
    $this->Mblog->addTag();
}

MODEL

public function addTag() {
    $lang = $this->input->post('lang');
    $tag  = $this->input->post('tag');

    $data = array(
        'tags' => $tag,
        'lang' => $lang
    );

    $this->blog->insert('tags', $data);

    return $this->blog->insert_id();
}
  • 写回答

3条回答 默认 最新

  • duanfengshang1088 2016-09-28 13:36
    关注

    Add return to your controller function or echo the result like as follows:

    public function add_tag() {
        return $this->Mblog->addTag();
    }
    

    OR

    public function add_tag() {
        echo json_encode(['data' => $this->Mblog->addTag()]);
    
        exit;
    }
    

    And then try to modify dump and see the ajax success response:

    $.ajax({
        url: "<?= site_url('blog/add_tag'); ?>",
        type: 'POST',
        data: data,
        dataType: 'json', // this will convert your results to json by default
        success: function (result) {
            // You should get the json result
            console.log(result.data);
    
            // Your regular logic to handle result
        }, 
        fail: function(res) {
            console.log(res); // will log fail results
        }
    });
    

    Try these and let us know if this works for you or not.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 本地运行的vue2项目,使用内网穿透,前端界面一直转圈圈
  • ¥20 powerbuilder datawindow控件导出Excel数据,可不可以不自动覆盖原数据,而是在后面新插入入数据。
  • ¥100 无轴承永磁同步电机控制
  • ¥15 eps里添加本地倾斜模型
  • ¥15 telegram 问题
  • ¥15 nrf52810-c三个a 程序
  • ¥15 lego-loam跑出来的roll误差很大
  • ¥50 求一个半透明没有锯齿的圆角窗体的实现例子
  • ¥15 STM32cubeMX里的FreeRTOS无法释放内存
  • ¥15 CATIA有些零件打开直接单机确定终止