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.
- <!DOCTYPE HTML>
- <html lang="">
- <head>
- <meta charset="UTF-8">
- <title>AXAX1</title>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
- <script>
- $(document).ready(function(){
- $('form').submit(function(e) {
- e.preventDefault();
- $.ajax({
- url: "AJAX2.php",
- type: "POST",
- data: $(this).serialize(),
- dataType: "json",
- success: function(data) {
- $('#display').html(data);
- },
- error: function(jqXHR, data ) {
- alert ('Ajax request Failed.');
- }
- });
- });
- });
- </script>
- </head>
- <body>
- <div class="container">
- <?php for($a = 0; $a < 3; $a++): ?>
- <form action="AJAX2.php" method="post">
- <input type="hidden" name="var" value="<?php echo $a; ?>">
- <input type="submit" name="submit" value="Index <?php echo $a; ?>">
- </form>
- <?php endfor; ?>
- </div>
- <div id="display">
- </div>
- </body>
- </html>
- <?php
- header('Content-Type: application/json');
- $variable = $_POST['var'];
- $img_dir = $variable . "/";
- foreach(glob($img_dir . '*.jpg') as $images) {
- echo json_encode ($images);
- }
- ?>
Thank you in advance.
.