I've read several posts on SO before asking my question. Many of them redirected me to the current thing i've right now. Its doing what its supposed, but it seems i'm missing something - because i can't figure out the final part.
My main goal is to submit a file using ajax (for preventing page reload) and then use its contents on another PHP file so i can process its data and display accordingly.
However, i can't figure out how to use the content inside the other php file. No matter what i try i can't find a way to capture the variable result (this variable contains my read file, i can see it trough the developer console).
Basically what i'm trying to achive is: 1)Load a php page with an form to submit an file 2) Prevent refresh after submiting the form 3) Process the submited data on the same page (like display an table with the content)
Thanks in advance!
loadData.php
<b>ARQUIVO</b><br>
<form id="formEnvioArquivos">
<input id="arquivo" name="blobFile" type="file">
<input type="button" id="submitFormData" onclick="enviaDadosForm()" value="Submit" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
function enviaDadosForm()
{
var myFile = $('#arquivo').prop('files')[0];
var fileReader = new FileReader();
fileReader.onLoad = function () {
var final = fileReader.result;
}
fileReader.readAsText(myFile);
var fd = new FormData(document.querySelector('#formEnvioArquivos'));
$.ajax ({
url: 'submit.php',
method: 'POST',
data: {'fileContent' : fileReader.result},
processData: false,
contentType: false,
success: function (data) {
console.log(myFile);
console.log(fileReader.result);
}
});
}
</script>
submit.php
<?php
echo 'FROM PHP: ', $_POST['arquivo'];
echo 'FROM PHP: ', $_FILES['arquivo']['tmp_name'];
?>
Ps: On this post: Submit form without page reloading I almost got what i wanted. However, i couldn't figure out how to pass the contents of the uploaded file instead of form fields.