нет сообщений об ошибках или предупреждений, поэтому я не знаю, почему это происходит.
вот функция javascript:
function agregarMiProducto(imagen){
/*=============================================
ALMACENAMOS TODOS LOS CAMPOS DE PRODUCTO
=============================================*/
var tituloProducto = $(".tituloProducto").val();
var rutaProducto = $(".rutaProducto").val();
var seleccionarTipo = $(".seleccionarTipo").val();
var seleccionarCategoria = $(".seleccionarCategoria").val();
var seleccionarSubCategoria = $(".seleccionarSubCategoria").val();
var descripcionProducto = $(".descripcionProducto").val();
var pClavesProducto = $(".pClavesProducto").val();
if(seleccionarTipo == "virtual"){
var detalles = {"Clases": $(".detalleClases").val(),
"Tiempo": $(".detalleTiempo").val(),
"Nivel": $(".detalleNivel").val(),
"Acceso": $(".detalleAcceso").val(),
"Dispositivo": $(".detalleDispositivo").val(),
"Certificado": $(".detalleCertificado").val()};
}else{
function getFirstTag(selector) {
var items = $(selector).tagsinput('items');
return Array.isArray(items) && items.length ? items[0] : "";
}
var detalles = {
"tamaño": getFirstTag(".detalleTamaño"),
"técnica": getFirstTag(".detalleTecnica"),
"año": getFirstTag(".detalleAño")
};
};
}
Object.keys(detalles).forEach(key => {
if (
detalles[key] === null ||
detalles[key] === "" ||
(Array.isArray(detalles[key]) && detalles[key].length === 0)
) {
delete detalles[key];
}
});
if (Object.keys(detalles).length === 0) {
swal({
type: "error",
title: "Debe ingresar al menos un detalle del producto",
showConfirmButton: true,
confirmButtonText: "Cerrar"
});
return;
}
var detallesString = JSON.stringify(detalles);
var datosProducto = new FormData();
datosProducto.append("tituloProducto", tituloProducto);
datosProducto.append("rutaProducto", rutaProducto);
datosProducto.append("seleccionarTipo", seleccionarTipo);
datosProducto.append("detalles", detallesString);
datosProducto.append("seleccionarCategoria", seleccionarCategoria);
datosProducto.append("seleccionarSubCategoria", seleccionarSubCategoria);
datosProducto.append("descripcionProducto", descripcionProducto);
datosProducto.append("pClavesProducto", pClavesProducto);
datosProducto.append("multimedia", imagen);
datosProducto.append("fotoPortada", imagenPortada);
datosProducto.append("fotoPrincipal", imagenFotoPrincipal);
$.ajax({
url:"ajax/productos.ajax.php",
method: "POST",
data: datosProducto,
cache: false,
contentType: false,
processData: false,
success: function(respuesta){
// console.log("respuesta", respuesta);
if(respuesta == "ok"){
swal({
type: "success",
title: "El producto ha sido guardado correctamente",
showConfirmButton: true,
confirmButtonText: "Cerrar"
}).then(function(result){
if (result.value) {
window.location = "productos";
}
})
}
}
})
}
вот форма PHP, в которой собираются «подробные» данные:
функция ajax:
public function mostrarTablaProductos(){
$item = null;
$valor = null;
$productos = ControladorProductos::ctrMostrarProductos($item, $valor);
$datosJson = '
{
"data":[';
for($i = 0; $i < count($productos); $i++){
/*=============================================
TRAER LAS CATEGORÍAS
=============================================*/
$item = "id";
$valor = $productos[$i]["id_categoria"];
$categorias = ControladorCategorias::ctrMostrarCategorias($item, $valor);
if(is_array($categorias) && $categorias["categoria"] != ""){
$categoria = $categorias["categoria"];
}else{
$categoria = "SIN CATEGORÍA";
}
/*=============================================
TRAER LAS SUBCATEGORÍAS
=============================================*/
$item2 = "id";
$valor2 = $productos[$i]["id_subcategoria"];
$subcategorias = ControladorSubCategorias::ctrMostrarSubCategorias($item2, $valor2);
if($subcategorias[0]["subcategoria"] == ""){
$subcategoria = "SIN SUBCATEGORÍA";
}else{
$subcategoria = $subcategorias[0]["subcategoria"];
}
/*=============================================
AGREGAR ETIQUETAS DE ESTADO
=============================================*/
if($productos[$i]["estado"] == 0){
$colorEstado = "btn-danger";
$textoEstado = "Desactivado";
$estadoProducto = 1;
}else{
$colorEstado = "btn-success";
$textoEstado = "Activado";
$estadoProducto = 0;
}
$estado = "".$textoEstado."";
/*=============================================
TRAER LAS CABECERAS
=============================================*/
$item3 = "ruta";
$valor3 = $productos[$i]["ruta"];
$cabeceras = ControladorCabeceras::ctrMostrarCabeceras($item3, $valor3);
if(is_array($cabeceras) && $cabeceras["portada"] != ""){
$imagenPortada = "

";
}
/*=============================================
TRAER IMAGEN PRINCIPAL
=============================================*/
$imagenPrincipal = "
";
}
}else{
$vistaMultimedia = "

";
}
/*=============================================
TRAER DETALLES
=============================================*/
$detalles = json_decode($productos[$i]["detalles"],true);
if(is_array($productos) && $productos[$i]["tipo"] == "fisico"){
$tamaño = json_encode($detalles["tamaño"]);
$tecnica = json_encode($detalles["técnica"]);
$año = json_encode($detalles["año"]);
$status = json_encode($detalles["status"]);
$vistaDetalles = "Tamaño: ".str_replace(array("[","]",'"'), "", $tamaño)." - Tecnica: ".str_replace(array("[","]",'"'), "", $tecnica)." - Año: ".str_replace(array("[","]",'"'), "", $año)." - Status: ".str_replace(array("[","]",'"'), "", $status);
}
/*=============================================
TRAER LAS ACCIONES
=============================================*/
$acciones = "";
/*=============================================
CONSTRUIR DATOS JSON
=============================================*/
$datosJson .= '[
"'.($i+1).'",
"'.$productos[$i]["titulo"].'",
"'.$categoria.'",
"'.$subcategoria.'",
"'.$productos[$i]["ruta"].'",
"'.$estado.'",
"'.$productos[$i]["tipo"].'",
"'.(is_array($cabeceras) ? $cabeceras["descripcion"] : 'Sin descripción').'",
"'.(is_array($cabeceras) ? $cabeceras["palabrasClaves"] : 'Sin palabras clave' ).'",
"'.$imagenPortada.'",
"'.$imagenPrincipal.'",
"'.$vistaMultimedia.'",
"'.$vistaDetalles.'",
"'.$acciones.'"
],';
}
$datosJson = substr($datosJson, 0, -1);
$datosJson .= ']
}';
echo $datosJson;
}
}
/*=============================================
ACTIVAR TABLA DE PRODUCTOS
=============================================*/
$activarProductos = new TablaProductos();
$activarProductos -> mostrarTablaProductos();
Подробнее здесь: https://stackoverflow.com/questions/798 ... s-properly
Мобильная версия