I'm a javascript and php beginner and I've been trying to get a solution for 2 days now and it might be easy but I couldn't find any solutions. So, i have my index.php with a file input and a button and I want that when the user clicks on the button it executes a js function that will call my excelparse.php file to process the uploaded file but I dont know how i can call the excelparse.php file on the function. How do I call the excelparse.php file on my on.click function?
//index.php
<form>
<div class="form-group">
<label for="ficheiro">Select a file</label>
<input type="file" class="form-control-file" id="ficheiro">
</div>
<button type="button" id="parse" class="btn btn-light">Parse</button>
</form>
//js.js
parse.on("click", function() {
});
//excelparse.php
$path_parts = pathinfo($_FILES["file"]["name"]);
$extension = $path_parts['extension'];
if ($extension == 'xls') {
$extension = 'Xls';
}
elseif ($extension == 'xlsx') {
$extension = 'Xlsx';
}
move_uploaded_file($_FILES['file']['tmp_name'],('files/tmp'));
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($extension);
$spreadsheet = $reader->load("files/tmp");
$sheet = $spreadsheet->getActiveSheet();
foreach ($sheet ->getRowIterator() as $row) {
$cellIterator = $row ->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$array = array();
foreach ($cellIterator as $cell) {
if (!is_null($cell)) {
$value = $cell->getFormattedValue();
$array[] = $value;
}
}
print_r($array);
}