The <input id="dir" is to enter the base directory to start finding the dir tree. ie: /home/rick/Desktop/testFolder. I use firebug to debug. All it says is dir="". I am not real good at using firebug so there may be things I am missing. If you want to run the program code is shown.
testFolder structure is:
testFolder
|_folder1___forder2___folder3___Pictures
|_anotherFolder |_folder1
|_img1
|_img2
|_img3
Output is as attachment: Without using <form>
tree2.html ####################################################
- <!DOCTYPE html>
<html>
<head><title>tree2.html Sun 02Jul2017</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<!-- ============== head scripts ================ -->
<script type="text/javascript" src="../jquery/jquery-2.1.1.js"></script>
<style>
.hov:hover {
color: red;
font-weight: bold;
}
li {
list-style-type:none;
}
</style>
</head>
<!-- ============== html ================ -->
<body>
<form action=""#"" method="POST">
Enter Base Directory to Choose Photo Directory From
<input type="text" id="dir">
<!--<input type="submit"> using .on(change, ... line 51 -->
</form>
<!-- ============== body scripts 1 of 1 ================ -->
<script type="text/javascript">
$(function(){ // ready
var dir="/home/rick/Desktop/testFolder"; // for testing works ok
//$("#tbl1").hide();
// CAN'T GET THIS TO WORK REGARDLESS OF WHERE I PUT IT
// IT CALLS AJAX BEFORE YOU CAN ENTER THE DATA IN INPUT
/*
var dir="";
$("document").on('change', "#dir", function () {
dir=$("input#dir").val();
});
*/
myAjaxCall(dir);
function myAjaxCall(dir) {
var request = $.ajax( {
url: "tree2.php",
type: "POST",
data: {"dir":dir},
dataType: "json"
}); // end ajax()
request.done(showDirs);
request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: line 74 myAjaxCall() " + textStatus );
});
} // end myAjaxCall()
}); // end ready()
// ========================== FUNCTION showDirs() ==================
function showDirs(data) {
var i=0;
var disp="";
var choice="";
$("#tbl1").show();
$("body").append("<h3> Pick A Directory to Process Photos</h3>");
table_str = "<table id='tbl1' class='center' id='verifyTbl' border='4' cellspacing='2' cellpadding='4' bgcolor='#fff99d' ><br/>";
$.each(data, function (index, value) {
//disp="disp"+i++;
table_str += "<tr><td class='disp hov'>" + value + "</td></tr>";
});
table_str += "</table><p></p><p></p>";
$("body").append(table_str + "<p></p>");
$("td.disp").click(function () {
console.log("testing");
choice=($( this ).text());
alert("You chose: " + choice);
// ajax call to getImageFiles.php goes here.
}); // end click()
} // end showDirs() ===================================================
</script>
</body>
</html>
tree2.php ####################################################
- <?php
/* ======================================================
A program to return a Directory tree.
=======================================================*/
set_include_path( '../../include' );
error_reporting (E_ALL ^ E_NOTICE);
$result="";
$tmpFile=array();
$dirs=$_POST['dir'];
doFiles($dirs);
function doFiles($dirs) {
global $tmpFile;
$tmpFile[]=$dirs;
if ($fh = opendir($dirs)) {
while (false !== ($file = readdir($fh))) {
if(in_array($file, array('.', '..'))) continue;
if(is_dir($dirs."/".$file)) {
$newDir=$dirs."/".$file;
doFiles($newDir);
} // is_dir
} // while
closedir($fh);
} // $fh
} // doFiles()
asort($tmpFile);
echo json_encode($tmpFile);
?>