Skip to content

Instantly share code, notes, and snippets.

@AndrewStanton94
Last active August 29, 2015 14:11
Show Gist options
  • Save AndrewStanton94/0ad24473e244efdb2e1e to your computer and use it in GitHub Desktop.
Save AndrewStanton94/0ad24473e244efdb2e1e to your computer and use it in GitHub Desktop.
Shows how ajaxGet can be used for .txt & .php. Full example. getdir can be called by passing its url as first arg of ajaxget.js same with d3sg.js
<!DOCTYPE HTML>
<html>
<head>
<title>ds3e</title>
<meta charset = 'UTF-8'>
<link rel='icon' href='favicon.ico'>
<script src='lib/ajaxget.js'></script>
<script src='ds3e.js'></script>
</head>
<body>
<h1>Dynasite 3e &ndash; First AJAX</h1>
<div id=grabdiv>
AJAX uploads go here.
</div>
<div id='grabphpdiv'>
AJAX uploads from PHP go here.
</div>
</body>
</html>
function AjaxGet(URL, callback)
{
var ajaxObj = new XMLHttpRequest();
ajaxObj.open("GET", URL, true); // The True implies asynchronous
ajaxObj.onreadystatechange = function()
{
if (ajaxObj.status == 200)
if (ajaxObj.readyState == 4)
callback(ajaxObj.responseText);
};
ajaxObj.send(null);
}
function grabber (response) {
document.getElementById('grabdiv').innerHTML = response;
}
function grabphp(response) {
document.getElementById('grabphpdiv').innerHTML = response;
}
AjaxGet('datafile.txt', grabber);
AjaxGet('api/ds3e.php', grabphp);
<?php echo "This text was requested by AJAX and ECHOed by ds3e.php"; ?>
function showFileNames (respose) {
console.log('respose = ' + respose);
namearray = respose.split(',');
console.log('namearray.length = ' + namearray.length);
for (var namepile="", i=0; i<namearray.length; i++) {
console.log('namearray[' + i + '] = ' + namearray[i]);
var ni = namearray[i];
if (ni.endsWith('.htm') || ni.endsWith('.html') || ni.endsWith('.php') || ni.endsWith('.js') || ni.endsWith('.txt'))
{
namepile = namepile + namearray[i] + "<br>";
console.log('namepile#' + i + ' = ' + namepile);
}
}
document.getElementById('filenamesdiv').innerHTML = namepile;
}
AjaxGet('api/getdir.php', showFileNames);
<?php
$dir = opendir('../');
if (!$dir) {echo "Error: could not open directory"; exit;}
while ($name=readdir($dir)) {
if (!is_dir($name) && !is_executable($name))
echo "$name";
}
echo "-\r\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment