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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!