0

This postprovides sample code using obtaining a server file list as an example. Here is the code I have used:

<html lang="en-US">
<head>
 <meta charset="UTF-8">
 <title>How to create form using jQuery UI</title>
 <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/pepper-grinder/jquery-ui.css" media="screen" rel="stylesheet" type="text/css"> 
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
 <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js" type="text/javascript"></script>
 <script type="text/javascript">
 $(function() {
 $.get('getfilename.php', { dir : '/ajax1/' }, function(data) {
 // you should now have a json encoded PHP array
 $.each(data, function(key, val) {
 alert('index ' + key + ' points to file ' + val);
 });
 }, 'json');
 alert ("ok");
});
 </script>
</head>
<body>
<h1>Get file names... </h1>
</body>
</html>

getfilename.php

$dir = $_REQUEST['dir'] ;
$dir = $_SERVER['DOCUMENT_ROOT'] . $dir;
$filesArray = array(); 
$Counter = 0; 
$files = scandir($dir); 
foreach ($files as &$file) { 
 if ($file!='.' && $file!='..' ) { 
 $filesArray[$Counter] = $file; 
 echo $filesArray[$Counter].''; 
 $Counter++;
 }
} 
echo json_encode($filesArray);

My problem is that the javascript alert alert('index ' + key + ' points to file ' + val); fails to display anything on the page. The script is working because I get a response in the Firebug console log.

ajax.jsajax.phpindex.html["ajax.js","ajax.php","index.html"]

What I need to change on the script to return this information to the html page so that I can use the JSON for further processing ?

Thanks.

asked Apr 25, 2016 at 22:56
2
  • what's the output of console.log(data); in your javascript console? Are there any console errors? Commented Apr 25, 2016 at 22:59
  • Thanks Robbert - I used Firebug and there are no errors shown in the Console section. Commented Apr 27, 2016 at 15:51

1 Answer 1

1

With your debug you broke the JSON output in PHP. So, remove:

echo $filesArray[$Counter].''; 

Also, before any output, you should add JSON header:

header('Content-Type: application/json');

At the end, your PHP file should look like this:

$dir = $_REQUEST['dir'] ;
$dir = $_SERVER['DOCUMENT_ROOT'] . $dir;
$filesArray = array(); 
$files = scandir($dir); 
foreach ($files as &$file) { 
 if ($file!='.' && $file!='..' ) { 
 $filesArray[] = $file; 
 }
}
header('Content-Type: application/json');
echo json_encode($filesArray);
answered Apr 25, 2016 at 23:11
Sign up to request clarification or add additional context in comments.

5 Comments

Since he specifies the datatype json when he calls $.get, the Content-Type header is redundant.
Your chicken is older than the egg, Barmar. If he specified the PHP header, than he would not care about the dataType later on.
But he already has that in the code in his question, so it's not needed to fix the code.
Thanks skobaljic - the alerts in the loop now put out indices and values. Brilliant ! Thanks also for the explanation and also for posting the corrected code.
You are welcome. You should also consider using glob function, it would return the array of files.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.