Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!