开发者

Returning SQL Insert ID when using Jquery/AJAX to insert row into database

Im trying to add an element to a database and then return a new id for it based on the the sql_insert_id

Its adding the element correctly and the console开发者_如何学C is returning the id i want to use but for some reason its not actually changing it on the element.

Im just using an echo in my actions.php script to return the string.

$(".add").click(function () {
    $(this).parent().parent().parent().parent().appendTo("#folderwrap");
    $(".thumbnailoverlay").hide();
    var imageidlong = $(this).parent().parent().parent().parent().attr("id");
    var imageid = imageidlong.replace("imageid_", "");
    $.post("actions.php?action=addfolderimage&imageid="+imageid, function(data){
        console.log(data);
        $(this).parent().parent().parent().parent().attr("id", data);
    });
});


In your callback $(this) won't refer to your element any more - try this instead:

$(".add").click(function () {
        var elm = $(this).parent().parent().parent().parent();

        elm.appendTo("#folderwrap");
        $(".thumbnailoverlay").hide();
        var imageidlong = elm.attr("id");
        var imageid = imageidlong.replace("imageid_", "");

        $.post("actions.php?action=addfolderimage&imageid="+imageid, function(data){
                console.log(data);
                elm.attr("id", data);
        });
});

This creates a closure containing elm

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜