TP5 framework based on Ajax to achieve asynchronous deletion of pictures method example

Time:2021-7-20

This paper describes the method of asynchronous deletion of pictures based on Ajax in TP5 framework. The details are as follows:

In order to improve the user experience, we have made Ajax non refresh asynchronous deletion function for the commodity album. The process and method are very worthy of reference. The effect is as follows:

In the picture list above, you can click the minus sign next to it to delete the picture not only from the web page, but also from the server without refreshing the current page. Let’s take a look at our core processing code

The first is the JS code of the client

function delrow(o){
If (confirm ('are you sure you want to delete this graph?){
var div=$(o).parent().parent();
var id=div.attr('id');
$.ajax({
type:"POST",
data:{id:id},
url:"{:url('Goods/ajaxdelpic')}",
success:function(data){
if(data==1){
div.remove();
}else{
Alert ('delete failed ');
}
}
});
}
}

When the request is sent to the server, we handle it as follows:

//Asynchronous deletion of product album pictures public function ajaxdelpic ($ID){
$gphoto=db('goods_photo');
$gphotos=$gphoto->find($id);
$ogPhoto=IMG_UPLOADS.$gphotos['og_photo'];
$bigPhoto=IMG_UPLOADS.$gphotos['big_photo'];
$midPhoto=IMG_UPLOADS.$gphotos['mid_photo'];
$smPhoto=IMG_UPLOADS.$gphotos['sm_photo'];
@unlink($ogPhoto);
@unlink($bigPhoto);
@unlink($midPhoto);
@unlink($smPhoto);
$del=$gphoto->delete($id);
if($del){
echo 1;
}else{
echo 2;
}
}

For more information about ThinkPHP, interested readers can check out the following topics: introduction to ThinkPHP, summary of operation skills of ThinkPHP template, summary of common methods of ThinkPHP, introduction to CodeIgniter, CI (CodeIgniter) framework advanced course, Zend framework introduction course and PHP template technology summary.

I hope this article will be helpful to the design of PHP program based on ThinkPHP framework.