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

280
Vistas
How do I get a value that is returned by a php script in Ajax?

I need to get an array $data from the backend to the frontend in order to organize it for viewing. I can only find examples using jQuery Ajax.

<!DOCTYPE html>
<html>

<style>
    .table_class{
        display:none;
    }
</style>
<script>
    async function uploadFile() {

            let formData = new FormData();
            formData.append("file", fileupload.files[0]);
            console.log(formData);

            await fetch('file_ingest.php', {
                method: "POST",
                body: formData,

            });
            var test = document.getElementById("test");
            test.innerText = ('The file has been uploaded successfully.');
            //var returned_vals = JSON.parse(formData.responseText);
            //console.log(returned_vals);



    }

</script>
<body>

<input id="fileupload" type="file" name="fileupload" />
<button id="upload-button" onclick="uploadFile()"> Upload </button>
<p id = "test"></p>
</body>
</html>

I need to get an array $data from the backend to the frontend in order to organize it for viewing. I can only find examples using jQuery Ajax.

<?php
set_time_limit(120);
/* Get the name of the uploaded file */
$filename = $_FILES['file']['name'];

/* Choose where to save the uploaded file */
$location = "uploads/".$filename;


if ( move_uploaded_file($_FILES['file']['tmp_name'], $location) ) {
    echo 'Success';
} else {
    echo 'Failure';


$CSVfp = fopen("uploads/${filename}", "r");
if($CSVfp !== FALSE) {
    while(! feoF($CSVfp)) {
        $data = fgetcsv ($CSVfp, 1000, ",");
        print json_encode($data);
        //echo $data;
    }
}
fclose($CSVfp);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

<?php
set_time_limit(120);    // if you need this you may have other problems

/* Get the name of the uploaded file */
$filename = $_FILES['file']['name'];

/* Choose where to save the uploaded file */
$location = "uploads/".$filename;

$response = [];

/* Save the uploaded file to the local filesystem */ 
if ( move_uploaded_file($_FILES['file']['tmp_name'], $location) ) {
    $response['status'] = 'Files Save Success';

    // read the file here, if it failed the upload and move 
    // there is no point trying to read it
    if (($fp = fopen($location, "r")) !== FALSE) {
        while (($data = fgetcsv($fp, 1000, ",")) !== FALSE) {
            $response['data'][] = $data    
        }
        fclose($fp);
    }

} else {
    $response['status'] = 'File Save Failure';
}

// now you can return either an error or a success and data
echo json_encode($response);

Now you need to checnge the javascript to look for the status in the right place and unload the row data from the the right place

about 4 years ago · Juan Pablo Isaza Denunciar
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