1
I have a page (up.php) that shows the following:
I want to do the following, by clicking Delete on any of them, show up a fa-Cog centered on the image and then remove it from there.
What I have so far does the job, but visually it’s bad:
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>
<style>
.meio {
width: 100%;
height: 100%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
</style>
<?php
$path = "img/";
$diretorio = dir($path);
while($arquivo = $diretorio -> read()) {
if (substr($arquivo, 0, 6) == 'modelo' ) {
$fotoscaminhocompleto[] = $path.$arquivo;
$nomearquivo[] = pathinfo($arquivo, PATHINFO_FILENAME);
$extarquivo[] = pathinfo($arquivo, PATHINFO_EXTENSION);
}
}
$diretorio -> close();
echo '<form method="post" id="apagarfoto" action="#">';
for ($x=0;$x<count($nomearquivo);$x++) {
echo '<div style="float: left; padding:5px;" align="center" id="'.$nomearquivo[$x].'">';
echo $nomearquivo[$x].'<br />';
echo '<input type="hidden" name="apagar" value="'.$nomearquivo[$x].'.'.$extarquivo[$x].'" />';
echo '<img src="'.$path.'/'.$nomearquivo[$x].'.'.$extarquivo[$x].'" width="100px" /><br />';
?><button type="submit" onclick="myAjax('<?php echo $nomearquivo[$x]; ?>');"><i class="fa fa-trash"></i> Apagar</button><br /><?php
echo '</div>';
}
echo '</form>';
?>
<script>
function myAjax(nome) {
var nomediv = nome;
$('#apagarfoto').submit(function(e) {
e.preventDefault();
var serializeDados = $('#apagarfoto').serialize();
$.ajax({
type: "POST",
url: "foto_apagar.php",
dataType: "html",
data: serializeDados,
beforeSend: function() {
document.getElementById(nomediv).innerHTML = '<div align="center"><i class="fa fa-spin fa-cog fa-3x"></i></div>';
},
complete: function() {
document.getElementById(nomediv).innerHTML = "";
},
success: function (data, textStatus) {
},
error: function(xhr, er) {
$("#resposta").html("Erro!");
}
});
});
};
</script>
And the PHP page that executes the "delete" (actually renames) is as follows:
<?php
error_reporting(0);
ini_set(“display_errors”, 0 );
$base_dir = 'img/';
$retorno = rename($base_dir.$_POST['apagar'], $base_dir.'renomeado_'.$_POST['apagar']);
//sleep(3);
/*
if ($retorno == 1) {
header("location: up.php");
} else {
echo 'Nao foi possivel executar a operacao. Entre em contato com o webmaster da pagina.';
}
*/
?>
I know it’s a bit confusing but what I want is the following: Search in the folder "img" the figures that start with the name "model" and display them for the user to delete them if you want. The user will think they have deleted but they will remain in the folder, will only be renamed and will no longer appear.
In need of some supplementation I am at your disposal!
Thank you in advance!
Just perfect! I don’t know much about javascript and ajax, but I already got a good idea of what can be done! Thank you very much!
– Gustavo