cambiar avatar y nombre en perfil.php
This commit is contained in:
+78
-44
@@ -1,42 +1,59 @@
|
||||
// =========================
|
||||
// MOSTRAR ESTADISTICAS
|
||||
// =========================
|
||||
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: "controladores/controladorUsuario.php",
|
||||
data: {
|
||||
orden: "mostrarEstadisticas",
|
||||
},
|
||||
}).done(function (respuesta) {
|
||||
console.log(respuesta);
|
||||
respuesta = JSON.parse(respuesta);
|
||||
if (respuesta.error) {
|
||||
console.log(respuesta.error);
|
||||
} else {
|
||||
$("#victorias").text(respuesta.victorias);
|
||||
$("#derrotas").text(respuesta.derrotas);
|
||||
$("#aciertos").text(respuesta.aciertos);
|
||||
}
|
||||
});
|
||||
|
||||
// =========================
|
||||
// CAMBIAR NOMBRE
|
||||
// =========================
|
||||
|
||||
function cambiarNombre() {
|
||||
let nuevoNombre = $("#nuevoNombre").val().trim();
|
||||
|
||||
const input = document.getElementById("nuevoNombre");
|
||||
const nombre = document.getElementById("nombre");
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: "controladores/controladorUsuario.php",
|
||||
data: {
|
||||
nombreUsuario: nuevoNombre,
|
||||
orden: "cambiarNombre",
|
||||
},
|
||||
}).done(function (respuesta) {
|
||||
console.log(respuesta);
|
||||
respuesta = JSON.parse(respuesta);
|
||||
if (respuesta.error) {
|
||||
console.log(respuesta.error);
|
||||
} else {
|
||||
// Actualizamos el nombre mostrado en la página
|
||||
$("#nombre").text(respuesta.nombreUsuario);
|
||||
|
||||
const nuevoNombre = input.value.trim();
|
||||
|
||||
if (nuevoNombre !== "") {
|
||||
|
||||
nombre.textContent = nuevoNombre;
|
||||
|
||||
input.value = "";
|
||||
// Limpiamos el input
|
||||
$("#nuevoNombre").val("");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// =========================
|
||||
// BORRAR NOMBRE
|
||||
// =========================
|
||||
|
||||
function borrarNombre() {
|
||||
|
||||
document.getElementById("nuevoNombre").value = "";
|
||||
|
||||
}
|
||||
|
||||
|
||||
// =========================
|
||||
// ABRIR SELECTOR DE IMAGEN
|
||||
// =========================
|
||||
|
||||
function seleccionarFoto() {
|
||||
|
||||
document.getElementById("inputFoto").click();
|
||||
|
||||
document.getElementById("nuevoNombre").value = "";
|
||||
}
|
||||
|
||||
|
||||
@@ -44,31 +61,48 @@ function seleccionarFoto() {
|
||||
// CAMBIAR FOTO
|
||||
// =========================
|
||||
|
||||
document.getElementById("inputFoto").addEventListener(
|
||||
"change",
|
||||
function(event) {
|
||||
function seleccionarFoto() {
|
||||
$("#inputFoto").click();
|
||||
}
|
||||
|
||||
const archivo = event.target.files[0];
|
||||
$("#inputFoto").change(function () {
|
||||
let archivo = this.files[0];
|
||||
|
||||
if (archivo) {
|
||||
if (!archivo) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Comprobamos que sea una imagen
|
||||
if (!archivo.type.startsWith("image/")) {
|
||||
// Comprobamos que sea una imagen
|
||||
if (!archivo.type.startsWith("image/")) {
|
||||
alert("El archivo seleccionado no es una imagen.");
|
||||
|
||||
alert("Por favor, selecciona una imagen.");
|
||||
$("#inputFoto").val("");
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let datos = new FormData();
|
||||
|
||||
// Creamos una URL temporal
|
||||
const imagenURL = URL.createObjectURL(archivo);
|
||||
|
||||
|
||||
// Cambiamos la foto
|
||||
document.getElementById("fotoPerfil").src = imagenURL;
|
||||
|
||||
}
|
||||
datos.append("avatar", archivo);
|
||||
datos.append("orden", "cambiarAvatar");
|
||||
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: "controladores/controladorUsuario.php",
|
||||
data: datos,
|
||||
processData: false,
|
||||
contentType: false,
|
||||
}).done(function (respuesta) {
|
||||
console.log(respuesta);
|
||||
respuesta = JSON.parse(respuesta);
|
||||
if (respuesta.error) {
|
||||
alert(respuesta.error);
|
||||
} else {
|
||||
// Actualizamos la imagen sin recargar la página
|
||||
$("#fotoPerfil").attr(
|
||||
"src",
|
||||
respuesta.avatar + "?t=" + new Date().getTime(),
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user