Ver Mensaje Individual
  #3 (permalink)  
Antiguo 18/09/2011, 12:05
alexg88
 
Fecha de Ingreso: abril-2011
Mensajes: 1.342
Antigüedad: 13 años
Puntos: 344
Respuesta: Array desde php con Ajax

Si te soy sincero, no veo cual es tu problema.

Por lo que he entendido tienes un formulario donde realizas una busqueda de ficheros y quieres que al darle al botón de Submit (el cual haría de tipo button, para ahorrarte el return false) te devuelva mediante AJAX los ficheros que cumplen las condiciones especificadas.

Con el código que tienes y reorganizando los ficheros ya lo tendrías.

Código HTML:
Ver original
  1. <form action="submits.php" method="post" id="form-searchfiles">
  2.          <label for="folderPath">Enter folder path containing the files</label>
  3.          <input name="folderPath" id="folderPath" type="text" placeholder="domains" required autofocus/><br/>
  4.  
  5.          <label for="fileExtensions">Enter file extension(s) to scan</label>
  6.          <input name="fileExtensions" id="fileExtensions" type="text" placeholder="txt" required autofocus/><br/>
  7.  
  8.          <input type="submit" name="searchfiles" id="searchfiles" value="Submit" />
  9. </form>
  10.                    
  11. <br/>
  12.  
  13. <div id="resultado"></div>

submits.php:

Código PHP:
Ver original
  1. <?php
  2. if(isset ($_POST['ajax']))
  3.     {
  4.         $folderPath = $_POST['folderPath'];
  5.         $fileExtensions = $_POST['fileExtensions'];
  6.          
  7.         if(!file_exists('libraries/fileExplorer.class.php')){
  8.             exit ('File does not exists');
  9.         }
  10.          
  11.         require ('libraries/fileExplorer.class.php');
  12.         $fileExplorer = new fileExplorer;
  13.         $files = $fileExplorer->showFiles($folderPath, $fileExtensions);            
  14.  
  15. ?>
  16. <div class="select">
  17.          <div class="title">Select the files you whant to process:</div>
  18.          <form method="post" id="form-selectfiles">
  19.               <input type="checkbox" onclick="toggleChecked(this.checked)"> Select / Deselect All<br/>
  20.               <div id="checkboxes">
  21.                   <?php foreach ($files as $file):?>
  22.                        <input type="checkbox" id="<?php echo $file; ?>" name="<?php echo $file; ?>"><?php echo $file; ?><br/>
  23.                   <?php endforeach;?>
  24.                </div>
  25.                 <input type="submit" name="selectfiles" id="selectfiles" value="Submit" />
  26.           </form>
  27. </div>
  28. <?php
  29. }
  30. ?>


Código Javascript:
Ver original
  1. $('#searchfiles').click(function() {
  2.  
  3.                     var folderPath = $('#folderPath').val();
  4.                     var fileExtensions = $('#fileExtensions').val();
  5.  
  6.                     if (!folderPath) {
  7.                         alert('You did not enter a foltder path');
  8.                         return false;
  9.                     }
  10.  
  11.                     if (!fileExtensions){                    
  12.                         alert('You did not enter file extension(s');
  13.                         return false;
  14.                     }
  15.  
  16.                     var form_data = {
  17.                         folderPath: $('#folderPath').val(),
  18.                         fileExtensions: $('#fileExtensions').val(),
  19.                         ajax: '1'
  20.                     };
  21.  
  22.                     $.ajaxSetup({
  23.                         cache: false ,
  24.                         timeout: 5000
  25.                     });
  26.                                        
  27.                     $.ajax({
  28.                         url: "submits.php",
  29.                         type: "POST",
  30.                         data: form_data,
  31.                         success: function(datos){
  32.                             $('#form-searchfiles').fadeOut('slow');
  33.                             $('.select').fadeIn('slow');                          
  34.                             $('#resultado').html(datos);
  35.                         },
  36.                         error:function(x,e){
  37.                             if(x.status==0){
  38.                                 alert('You are offline!!\n Please Check Your Network.');
  39.                             }else if(x.status==404){
  40.                                 alert('Requested URL not found.');
  41.                             }else if(x.status==500){
  42.                                 alert('Internel Server Error.');
  43.                             }else if(e=='parsererror'){
  44.                                 alert('Error.\nParsing JSON Request failed.');
  45.                             }else if(e=='timeout'){
  46.                                 alert('Request Time out.');
  47.                             }else {
  48.                                 alert('Unknow Error.\n'+x.responseText);
  49.                             }
  50.                         }
  51.                     });
  52.  
  53.                     return false;
  54.  
  55.                 });
  56.  
  57.  
  58.                 function toggleChecked(status) {
  59.                     $("#checkboxes input").each( function() {
  60.                         $(this).attr("checked",status);
  61.                     });
  62.                 };