Carlos Ortiz Posted August 22, 2017 Share Posted August 22, 2017 (edited) Estoy subiendo un archivo csv en una página llamada procesar, el problema es que me hace un POST y me envía a otra página y me muestra el resultado de ¡La importación del archivo se subió satisfactoriamente!, pero yo necesito que eso me lo muestre en la misma página. Éste es el código que tengo. <script type="text/javascript" > $(function(){ $("#btnEnviar").click(function(){ $("#cuerpoPagina").load('procesar.php',$("#frmAsignar").serialize()); }) }); function subirArchivoExcel() { if (document.frmSubirArchivo.excel.value == "") { alert("¡Error!. ¡Debe subir un archivo!"); document.frmSubirArchivo.excel.focus(); return false; } document.frmSubirArchivo.action="procesar.php"; document.frmSubirArchivo.submit(); } </script> <?php echo '<form name="frmSubirArchivo" action"procesar.php" method="post" enctype="multipart/form-data"> <table border="1" id="TablaFormularioOpaco"> <tr> <td aling="center" id="TituloTablaVerde">Archivo excel</td> </tr> <tr> <td><input type="file" name="excel" /> </td> </tr> <tr> <td><input type="button" name="btnEnviar" id="btnEnviar" value="Subir Archivo" onclick="subirArchivoExcel();" ></td> </tr> </form>'; Ahora acá es el resto del código: include 'Conexion.php'; if (isset($_FILES['excel']) && !empty($_FILES['excel']) && substr($_FILES['excel']['name'],-3)=="csv") { $fecha = date('Y-m-d'); $hora = time("HH:MM:SS"); $carpeta = "Ejemplo"; $carpeta = ""; $excel = $fecha."-".$hora."-".$_FILES['excel']['name']; move_uploaded_file($_FILES['excel']['tmp_name'], "$carpeta$excel"); //echo "$carpeta$excel"; //print_r($_FILES); $contador = 0; $row = 1; $fp = fopen("$carpeta$excel", "r"); $meses = array( 'ENERO' => '01', 'FEBRERO' => '02', 'MARZO' => '03', 'ABRIL' => '04', 'MAYO' => '05', 'JUNIO' => '06', 'JULIO' => '07', 'AGOSTO' => '08', 'SEPTIEMBRE' => '09', 'OCTUBRE' => '10', 'NOVIEMBRE' => '11', 'DICIEMBRE' => '12'); //echo $dias; //fgetcsv. obtiene los valores que estan en el csv y los extrae while ($data = fgetcsv($fp, 1000, ";")) { //Si la línea es igual a 5 no guardamos porque serian los títulos de la hoja de excel if ($row >= 7) { //print_r("<pre>Row: ".$row); //print_r($data); //print_r("</pre>"); // TEXTO: jueves, 01 de junio de 2017 21:38 $laFecha = explode(",", $data[1]); if(count($laFecha) == 2) { $fecArchivo = explode(" ", $laFecha[1]); //print_r($fecArchivo); $anno = $fecArchivo[5]; $mes = $meses[strtoupper($fecArchivo[3])]; $dia = $fecArchivo[1]; $hora = $fecArchivo[6]; $fechaReal = $anno."-".$mes."-".$dia; //echo $fechaReal; //echo $contador." - "; $sql_guardar = 'INSERT INTO registros ( Dia, Hora, Tipo_Acceso, Puerta, Tipo, Tarjeta, Nombres, Apellidos, Documento, Empresa)'; $sql_guardar .= 'VALUES( "'.$fechaReal.'", "'.$hora.'", "'.$data[2].'", "'.$data[3].'", "'.$data[6].'", "'.$data[7].'", "'.$data[8].'", "'.$data[9].'", "'.$data[10].'", "'.$data[12].'")'; $contador++; //echo "7: ->".$data[7]."<- 10:->".$data[10]."<-"; if ($data[7] != '') { //echo $sql_guardar; //print_r("<pre>"); // print_r($sql_guardar); //print_r("</pre>"); mysql_query($sql_guardar) or die(mysql_error()); if (!$sql_guardar) { echo '<div>¡Hubo un problema al momento de importar el archivo! <br>¡Por favor vuelva a intentarlo!</div>'; exit; } } } } $row++; } fclose($fp); echo '<div>¡La importación del archivo se subió satisfactoriamente!</div>'; exit; } ?> Edited August 22, 2017 by Carlos Ortiz Link to comment Share on other sites More sharing options...
justsomeguy Posted August 22, 2017 Share Posted August 22, 2017 If you want to upload a file using ajax, the best way to do it is to use the FileReader object. https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now