doumengjing1500 2014-11-26 12:13
浏览 46
已采纳

用ajax删除sql表数据

i have stored image data in a sql table when uploading images. then i want to delete individual images with ajax.

here is my php code to retrieve images from table. there are few images.

while($infoi= mysqli_fetch_array($ri, MYSQLI_ASSOC)){

    $imageId= $infoi['image_id'];
    $albumIdi= $infoi['album_id'];
    $imageName= $infoi['image_name'];
    $size= $infoi['size'];
    $type= $infoi['type'];
    $mainImage= $infoi['main_image_file_path'];
    $thumbImage= $infoi['thumb_image_file_path'];
    $addedDayi= $infoi['added_day'];


    echo '  <tr class="'.$imageId.'">
            <td> <a href="'.$mainImage.'" data-lightbox="'.$albumIdi.'">   <img src="'.$thumbImage.'" class="img-responsive thumbnail"  />    </a></td>
            <td> '.$imageName.' </td>
            <td> '.$size.' Kb  </td>
            <td>'.$addedDayi.'</td>
            <td> 
                <input type="submit" class=" btn btn-sm btn-danger ico-windows" id="'.$imageId.'" value="Delete"/>

                <div class="showResult" id="'.$imageId.'" style="display:none"> this division hided </div>
            </td> 
        </tr>
          ';  
    }// inner while

how i pass the image id via ajax to the php script? in php i can use query string to pass the imageId but in ajax i have no idea to how to it.

if deleted successfully whole table row should be hided and if not deleted an error message should be showed in div class="showResult"

展开全部

  • 写回答

1条回答 默认 最新

  • doubi4814 2014-11-26 12:26
    关注

    You can send it as a get query too. You just need to append the id to your url

    var url = url + "?imageId=" + id;
    

    if you use jQuery it's very simple.

    $("#select inputs").click(function() {
        var id = $(this).attr("id");
        $.ajax({
            url : "http://my.website.com/ajax-delete.php?imageId=" + id,
            success : function() {
                $("#" + id).hide();
            }
        });
    }
    

    I forget the confirm part. With jQuery you can add a callback function who can hide the element if the query succeed. I edited to add a closure.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部