Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

279
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda