Help with a code to get file location dynamically

Help with a code to get file location dynamically

Hello. I'm new to jquery. I'm trying to make a code for opening a directory dynamically with ajax. In my document I have two pages and multiple sequentially numbered folders.
In AJAX1.php I have a form that sends a numerical variable. In AJAX2.php I have a glob function associated with this numerical variable. I'm using json to decode the PHP data properly.
I'm trying to open this directory in AJAX1.php again but I do not have success with this.

  1. <!DOCTYPE HTML>
  2. <html lang="">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>AXAX1</title>
  6.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  7.     <script>
  8.         $(document).ready(function(){
  9.         $('form').submit(function(e) {
  10.             e.preventDefault();
  11.             $.ajax({
  12.                 url: "AJAX2.php",
  13.                 type: "POST",
  14.                 data: $(this).serialize(),
  15.                 dataType: "json",
  16.                 success: function(data) {
  17.                     $('#display').html(data);
  18.                 },
  19.                 error: function(jqXHR, data ) {        
  20.                     alert ('Ajax request Failed.');    
  21.                 }
  22.             });
  23.         });
  24.     });
  25.     </script> 
  26. </head>
  27. <body>
  28. <div class="container">
  29. <?php for($a = 0; $a < 3; $a++): ?>
  30. <form action="AJAX2.php" method="post">
  31. <input type="hidden" name="var" value="<?php echo $a; ?>">
  32. <input type="submit" name="submit" value="Index <?php echo $a; ?>">
  33. </form>   
  34. <?php endfor; ?>
  35. </div>
  36. <div id="display">
  37. </div>    
  38. </body>
  39. </html>


  1. <?php
  2. header('Content-Type: application/json');
  3. $variable = $_POST['var'];  
  4. $img_dir = $variable . "/";
  5. foreach(glob($img_dir . '*.jpg') as $images) {
  6. echo json_encode ($images);    
  7. }
  8. ?>
Thank you in advance.