Upload select within a file upload

0

Hello good afternoon as you found I wanted to ask how I could upload a select within a file upload I mean upload the select and files with a single button this is the code I have now this is my javascript

//funcion cargueArchivo
    $(':file').change(function()
     {
         //obtenemos un array con los datos del archivo
         var file = $("#imagen")[0].files[0];
         //obtenemos el nombre del archivo
         var fileName = file.name;
         //obtenemos la extensión del archivo
         fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
         //obtenemos el tamaño del archivo
         var fileSize = file.size;
         //obtenemos el tipo de archivo image/png ejemplo
         var fileType = file.type;
         //mensaje con la información del archivo
         showMessage("<span class='info'>Archivo para subir: "+fileName+", peso total: "+fileSize+" bytes.</span>");
     });




    $('#cargueArchivo').click(function(){
        //información del formulario
        var formData = new FormData($("#frmFormulario")[0]);
        var message = "";
        //hacemos la petición ajax
        $.ajax({
            url: 'php/upload.php',
            type: 'POST',
            // Form data
            //datos del formulario
            data: formData,
            //necesario para subir archivos via ajax
            cache: false,
            contentType: false,
            processData: false,
            //mientras enviamos el archivo
            beforeSend: function(){
                message = $("<span class='before'>Subiendo la imagen, por favor espere...</span>");
                showMessage(message);
            },
            //una vez finalizado correctamente
            success: function(data){
                message = $("<span class='success'>La imagen ha subido correctamente.</span>");
                showMessage(message);
                if(isImage(fileExtension))
                {
                    //$(".showImage").html("Imagen Cargada: <img height ='500' src='/../files/"+data+"' />");


                    //alert('Se ha guardado correctamente el registro: ' + data);
                    $.post('php/imagen_actualiza.php', data, function(data2, textStatus, xhr) {
                        //alert('Data2: ' + data2);
                        $(".showImage").html(data2);

                    });
                    return;

                                        //showMessage("");
                                        //$('#cargueArchivo').prop('disabled',false);
                                        //$(':files').prop('disabled',false);

                }
            },
            //si ha ocurrido un error
            error: function(){
                message = $("<span class='error'>Ha ocurrido un error.</span>");
                showMessage(message);
            }
        });
    });
    //FIN funcion cargueArchivo
    //funcion incumplimiento

    //FIN funcion incumplimiento

}

THIS IS MY PHP     

//comprobamos que sea una petición ajax
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{

    //obtenemos el archivo a subir
    $file = $_FILES['archivo']['name'];

    //comprobamos si existe un directorio para subir el archivo
    //si no es así, lo creamos
    if(!is_dir("../files/"))
        mkdir("../files/", 0777);

    //comprobamos si el archivo ha subido
    if ($file && move_uploaded_file($_FILES['archivo']['tmp_name'],"../files/".$file))
    {
        sleep(3);//retrasamos la petición 3 segundos
        echo $file;//devolvemos el nombre del archivo para pintar la imagen
        //Insertar BD
            $image =  $_FILES['archivo']['name'];
            // Get text
            //$image_text = mysqli_real_escape_string($db, $_POST['image_text']);

            //$sql = "INSERT INTO images (image, image_text, id_inspeccion) VALUES ('$image', '$image_text','". $_SESSION['id_inspeccion'] ."')";
            $sql = "INSERT INTO images (image, image_text, id_inspeccion) VALUES ('$image', '$image_text','". $_SESSION['id_inspeccion'] ."')";
            $result = $link->query($sql);
            // execute query
            //mysqli_query($db, $sql);

       //Fin.Insertar BD
    }
}else{
    throw new Exception("Error Processing Request", 1);
}
?>

AND THIS SELECT THAT VISUALIZES

<?php
//session_start();
//include "conectar.php";
$sql_img = "SELECT * FROM images WHERE id_inspeccion = ". $_SESSION['id_inspeccion'];
//$link = conectar();
$num_rows_img =0;
$result_img = $link->query($sql_img);
$vector_resultado_img  = array();    
$content_img = '';

//$num_rows_img = $num_rows_img + NVL($result_img->num_rows,0);

if (isset($result_img->num_rows)) {

$content_img = '<table  width="55%"  rules="all" border="1" align="center" class="table table-bordered table-hover">
<thead>                                
<th>images</th>
<th></th>
</thead>';

while($row_img = $result_img->fetch_assoc()) 
   {
    $content_img .= '<tr><td> <img height ="500" src="files/'. $row_img["image"] .'" /></td>'; ///imagen                                        
    $content_img .=  '<td>'. $row_img["image_text"] . '</td>';  //descripcion
    $content_img .=  '</tr>';   
    }
}
$content_img .=  '</table>';

 echo $content_img;
?>

Thank you very much in advance

    
asked by JSACTM Music 17.09.2018 в 20:59
source

1 answer

0

If I understand you correctly, I guess what you want is to give the subit, pass to the php a sql, that is the string of the sql?

If yes, in your formData you make a append and put the string there.

var consulta = "SELECT lo que sea FROM tabla WHERE x";
formData.append("consultasql", consulta);
    
answered by 17.09.2018 в 21:22