I have the following problem, it is a page where the products have an image, I want to change the image, replacing it in the source folder, that works for me. What does not work for me is how to update the new image (which has the same name and is in the same folder that was replaced) without reloading the page
Image code:
<a href="#" id="editImg">Cambiar imagen</a>
<label>Codigo:</label><span id="viewCodigo">PROD001</span>
<img src="images/PROD001.jpg" id="viewImg">
Modal to change image is with jquery-ui:
<div id="mdlEditImg" title="Cambiar imagen" style="display:none;">
<form action="" role="form" enctype="multipart/form-data" id="formEditImg" name="formEditImg">
<label for="editImagen">Elegir imagen:</label><br>
<input type="file" name="editImagen" id="editImagen" class="archivo" accept="image/jpeg, .jpg, .png" style=""><br>
<input type="submit" name="editar" id="submitEditImg" value="Cambiar imagen">
<a href="#" id="editclose">Cancelar</a>
</form>
</div>
Jquery code:
$('#mdlEditImg').dialog({
autoOpen: false,
modal: true,
width: 450,
height: 'auto',
resizable: false,
close: function(){
$('#editImagen').val("");
}
});
$('#formEditImg').submit(function(e){
var image = $('#editImagen')[0].files[0];
var imageName = image.name;
var exten = imageName.substring(imageName.lastIndexOf('.') + 1);
var datos = new FormData(document.getElementById('formEditImg'));
datos.append("accion", "cambiarImg");
datos.append("exten", exten);
datos.append("codigo", $('#viewCodigo').text());
$.ajax({
type: "POST",
url: "productos-ajax.php",
data: datos,
cache: false,
contentType: false,
processData: false,
dataType: "html",
beforeSend: function(){
},
error: function(){
alert("Error en el servidor");
},
success: function(data){
if (data == "true") {
alert("Imagen cambiada correctamente!");
} else {
alert("Error al cambiar la imagen");
}
}
});
e.preventDefault();
});
Php code that replaces the image:
if ($_POST['accion'] == "cambiarImg") {
$result = "";
$carpeta = "images/";
if (is_dir($carpeta)) {
#Reemplazamos la imagen anterior pero con el mismo nombre que viene a ser el codigo del producto
if (!move_uploaded_file($_FILES['editImagen']['tmp_name'], $carpeta.$_POST['codigo'].".".$exten)){
$result = "Ocurrió algún error al subir el fichero. No pudo guardarse.";
} else {
$result = "true";
}
} else {
$result = "no existe la carpeta";
}
echo $result;
}
What I want is to update the new image that I replaced without having to reload the page because the change arises after updating the page, thanks in advance and I hope you have understood me. : P