Jump to content

LTMH38

New Members
  • Posts

    6
  • Joined

  • Last visited

LTMH38's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I've sent the form data to AJAX2.php properly indicating JSON data in this page and I've used the callback function to insert the data back in the page AJAX1.php with a jquery html method but the page display an alert with a failed request. $.ajax({ url: "AJAX2.php", type: "POST", data: $('form').serialize, dataType: "json", success: function(data) { $('#display').html(data); }, error: function(jqXHR, data ) { alert ('Ajax request Failed.'); } }); <body> ... <div id="display"> </div> </body> <?php header('Content-Type: application/json'); $variable = $_POST['var']; echo json_encode($variable); ?> Why does this happen?
  2. Hi. I've been learning ajax with jquery and I am struggling to work out how to pass PHP variable between two PHP pages. I started with a simple form: <!DOCTYPE HTML> <html lang=""> <head> <meta charset="UTF-8"> <title>FORM1</title> </head> <body> <div class="container"> <?php for($a = 0; $a < 3; $a++): ?> <form action="FORM2.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> </body> </html> <?php header('Content-Type: text/html; charset=utf-8'); $variable = $_POST['var']; echo $variable; ?> How can I do the same without page refresh using ajax? I have tried using json encoder but I haven't been successful. <!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", dataType: "json", success: function(result) { alert(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> </body> </html> <?php header('Content-Type: text/html; charset=utf-8'); $variable = $_POST['var']; echo json_encode($variable); ?>
  3. In the same directory I have two php documents and a folder called "gallery". This folder contain a lot of images files. I send a variable from the first page with the folder name. In the second page I open the directory and read directory content with glob function. I have tried to send images back to the first page again but images do not appear properly. I take a tutorial from this page: http://www.kvcodes.com/2013/12/get-all-images-from-a-directory-dynamically-php-jquery-ajax/ gallery.php <!DOCTYPE HTML> <html lang=""> <head> <meta charset="UTF-8"> <title>Gallery</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script> //start jquery $(document).ready(function(){ //submit from form $('form').submit(function(e) { e.preventDefault(); //start ajax with jquery $.ajax({ url: "directory.php", type: "POST", data: {'var': $('form input[name="var"]').val()}, dataType: "HTML", success: function( data ) { $('body').append(data); }, error: function(jqXHR, data ) { alert ('Ajax request Failed.'); } }); }); }); </script> </head> <body> <div class="content"> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam felis mi, pellentesque at scelerisque eu, consectetur quis felis. Aliquam mollis </p> </div> <div class="form"> <!--directory variable--> <?php $a = "gallery"; ?> <form action="directory.php" method="post"> <input type="hidden" name="var" value="<?php echo $a; ?>"> <input type="submit" name="submit" value="gallery"> </form> </div> <!--to display ajax result with colorbox--> <div class="gallery"> </div> </body> </html> directory.php <!DOCTYPE HTML> <html lang=""> <head> <meta charset="UTF-8"> <title>image directory</title> </head> <body> <?php //get the path of images directory $variable = $_POST['var']; $img_dir = $variable . "/"; //loop to bring all images inside directory foreach(glob($img_dir . '*.jpg') as $images) { echo '<img src="'.$img_dir.$images.'">'; } ?> </body> </html> Any guidance would be appreciated. Thanks in advance.
  4. Thank Psycho and Barand. I had tried with array_push: array_push ($DIR, $file); to display array values I had tried with a pair of loops: for($d = 0; $d <= count($DIR); $d++) { for($e = 0; $e <= count($DIR[$d]); $e++) { echo $DIR[$d][$e] . "<br>"; } } Unfortunately array values are not properly show. Also, a notice indicate undefined offset .
  5. I have a list of folder names like 1, 2, 3... Each folder has some .jpg files. I get a list of all including files from this directories with two glob functions dynamically. I store a list of directory paths and filenames into two separates arrays called $DIR[] and $files[]. How can I convert this arrays to one multidimensional array and how can I get values in each dimension with this array? <?php //FOLDERS //create array for each directory $pathDIR = "dirname/"; $DIR = Array( ); //store directory names into an array with foreach foreach (glob($pathDIR . '/[0-30]*', GLOB_ONLYDIR) as $dirname) { $DIR[] = $dirname; } //display path directories for($a = 0; $a < count($DIR); $a++) { echo $DIR[$a] . "<br>"; } //FILES //create array for each directory $pathFILE = Array(); //create array for files $files = Array(); //loop start for($b = 1; $b <= count($DIR); $b++) { $pathFILE[$b] = "dirname/" . $b . "/"; //store files in array with foreach foreach(glob($pathFILE[$b] . '*.jpg') as $filename) { $files[] = $filename; } } //display files for($c = 1; $c <= count($files); $c++) { echo $files[$c-1] . "<br>"; } //convert arrays to multidimensional array array_push ($DIR, $files); ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.