Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
How to launch ajax function based on a condition in a javascript function

I'm trying to launch an ajax call based on a condition (folder not empty) .

The result of my condition is working fine in php file check_file.php

Here is my check_file.php how it looks like

<?php

if (!empty($_POST["folder"])) 
{
$dir = $_POST['folder'];
$files = array_diff(scandir($dir), array('.', '..'));
$result;

if (!is_dir_empty($dir) && trim(substr( json_encode($files), 6, 2 )) == 'ok'  )  
{ 
$result = 0;  
}
else  {$result = 1;}

echo $result;
unlink($dir.'/'.trim($files[2])  )  ; 
}

function is_dir_empty($dir) {
if (!is_readable($dir)) return null; 
return (count(scandir($dir)) == 2);
}
?> 

In my JavaScript script this is how it looks like

function downloadExcel() {
// Mu folder path 
var folder="D:/output";
if (result.isConfirmed) {
               $.ajax({
                    type: 'POST',
                    url: 'check_file.php',
                    data: {
                        folder: folder
                    },
                    cache: false,
                    success: function(result){
                        if(result==0)
                        {
                            Swal.fire(
                                    'Execution  success , please check your email '
                            );
                        }
                        else
                        {
                            Swal.fire(
                                'Execution  en cours   '
                            );  
                        }
                        
                },
            error: function(result){

               console.log("Error request Ajax");
            } 
            });
            
            refresh();

        } else 
            result.dismiss === Swal.DismissReason.cancel
        }

I'm new to javascript , how could i test if folder is not empty launch the $.Ajax call

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda