//replace with your own function ...
var i = 0;
function move() {
if (i == 0) {
i = 1;
var elem = document.getElementById("myBar");
var width = 1;
var id = setInterval(frame, 10);
function frame() {
if (width >= 100) {
clearInterval(id);
i = 0;
} else {
width++;
elem.style.width = width + "%";
}
}
}
}
#myProgress {
width: 100%;
background-color: #ddd;
}
#myBar {
width: 1%;
height: 30px;
background-color: #04AA6D;
}
<h1>JavaScript Progress Bar</h1>
<div id="myProgress">
<div id="myBar"></div>
</div>
<br>
<button onclick="move()">Click Me</button>
here is how you do an a sync request
function produceCSV(data)
{
let csvContent = "data:text/csv;charset=utf-8,";
//unstring the data
data = JSON.parse(data);
data.forEach(function(rowArray) {
let row = rowArray.join(",");
csvContent += row + "\r\n";
});
var encodedUri = encodeURI(csvContent);
window.open(encodedUri);
}
//simple Async fetch request
fetch('http://time.jsontest.com/mydatafile.php')
.then(res => res.json())
.then((data) => {
console.log(data);
produceCSV(data);
}).catch(err => console.error(err));
Just make sure your php file returns json_encode($data);
...
And you might want to place some headers in that php file ... like
<?php
// Headers
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header('Access-Control-Allow-Methods: FETCH');
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
header('X-Requested-With: XMLHttpRequest');
header('Accept: application/json');
include_once 'connection.php';
include_once 'models.php';
//MYSQL CALL
$result = mysqlQuery ....
// convert the array to json string
$result = json_encode($result);
//Echoing data to the screen sends it to the clients browser in a post
request ...
echo $result;
If you ever need to find anything out about a JavaScript variable
console.log(yourJSvariable)
is your friend