I have the following code in one file (invasions.php):
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
<script type='text/javascript' src='script.js'></script>
</head>
<body>
<div id='output'>
<?php
$url = "my api link here";
$data = json_decode(file_get_contents($url));
if(!empty($data->invasions)) {
foreach ($data->invasions as $title => $inv) {
print "<h3 style='text-align:center;margin:auto;'><b>District:</b> {$title}</h3><br style='font-size:1px;'><h3 style='text-align:center;margin:auto;'><b>Cog:</b> {$inv->type}</h3><br style='font-size:1px;'><h3 style='text-align:center;margin:auto;'><b>Progress:</b> {$inv->progress}</h3>";
if (count(($data->invasions) > 1)) {
if(end($data->invasions) !== $inv){
print "<hr>";
} else {
print "<br style='font-size:2px;'>"; }
}
}
} else {
echo "<h1 style='text-align:center;margin:auto;padding:2px;color:darkred;font-weight:bold;'>No invasions!</span>";
}
?>
</div>
</body>
</html>
In my invasionapi.php, I have:
<?php
$assocArray = "https://www.toontownrewritten.com/api/invasions";
echo (file_get_contents($assocArray));
?>
In my script.js, I have:
$(function(){
function getData(){
$.post('invasions.php', function(data){
// var htm = do a bunch of stuff with the data Object, creating HTML
$('#output').html(htm);
});
}
setInterval(getData, 10000); // query every 10 seconds
});
I'm looking for some help to convert this php code to javascript so I can make this data update every 10 seconds through ajax. On a previous question, I received this response, but I don't know how to do it. I'd appreciate any help I could get. I've never ran data through javascript, or used ajax, I'm not sure where to start.