Foros del Web » Programando para Internet » Javascript » Frameworks JS »

Array desde php con Ajax

Estas en el tema de Array desde php con Ajax en el foro de Frameworks JS en Foros del Web. Buenos dias amigos, a ver si me pueden hechar una mano con lo siguiente. Estoy trabjando en una pequena aplicacion de interactuacion con ficheros de ...
  #1 (permalink)  
Antiguo 16/09/2011, 06:06
 
Fecha de Ingreso: junio-2009
Ubicación: Madrid
Mensajes: 402
Antigüedad: 14 años, 10 meses
Puntos: 20
Array desde php con Ajax

Buenos dias amigos, a ver si me pueden hechar una mano con lo siguiente.

Estoy trabjando en una pequena aplicacion de interactuacion con ficheros de Cpanel y la db.

Tengo esto
Código HTML:
<form action="submits.php" method="post" id="form-searchfiles">
         <label for="folderPath">Enter folder path containing the files</label>
         <input name="folderPath" id="folderPath" type="text" placeholder="domains" required autofocus/><br/>

         <label for="fileExtensions">Enter file extension(s) to scan</label>
         <input name="fileExtensions" id="fileExtensions" type="text" placeholder="txt" required autofocus/><br/>

         <input type="submit" name="searchfiles" id="searchfiles" value="Submit" />
</form>
                    
<br/>
                    
<div class="select">
         <div class="title">Select the files you whant to process:</div>
         <form method="post" id="form-selectfiles">
              <input type="checkbox" onclick="toggleChecked(this.checked)"> Select / Deselect All<br/>
              <div id="checkboxes">
                  [B]<?php foreach ($files as $file):?>
                       <input type="checkbox" id="<?php echo $file; ?>" name="<?php echo $file; ?>"><?php echo $file; ?><br/>
                  <?php endforeach;?>[/B]
               </div>
                <input type="submit" name="selectfiles" id="selectfiles" value="Submit" />
          </form>
</div> 
y esto otro

Código HTML:
$('#searchfiles').click(function() {

                    var folderPath = $('#folderPath').val();
                    var fileExtensions = $('#fileExtensions').val();

                    if (!folderPath) {
                        alert('You did not enter a foltder path');
                        return false;
                    }

                    if (!fileExtensions){                    
                        alert('You did not enter file extension(s');
                        return false;
                    }

                    var form_data = {
                        folderPath: $('#folderPath').val(),
                        fileExtensions: $('#fileExtensions').val(),
                        ajax: '1'
                    };

                    $.ajaxSetup({
                        cache: false ,
                        timeout: 5000
                    });
                    
                    //var ajax_load = "<img src='resources/images/ajax-loader.gif' alt='loading...' />";
                    
                    $.ajax({
                        url: "submits.php",
                        type: "POST",
                        data: form_data,
                        success: function(html){
                            $('#form-searchfiles').fadeOut('slow');
                            $('.select').fadeIn('slow');
                            alert(html);
                            //$('#content').html(ajax_load).append('<div id="result">' + html + '</div>').fadeIn();
                        },
                        error:function(x,e){
                            if(x.status==0){
                                alert('You are offline!!\n Please Check Your Network.');
                            }else if(x.status==404){
                                alert('Requested URL not found.');
                            }else if(x.status==500){
                                alert('Internel Server Error.');
                            }else if(e=='parsererror'){
                                alert('Error.\nParsing JSON Request failed.');
                            }else if(e=='timeout'){
                                alert('Request Time out.');
                            }else {
                                alert('Unknow Error.\n'+x.responseText);
                            }
                        }
                    });

                    return false;

                });


                function toggleChecked(status) {
                    $("#checkboxes input").each( function() {
                        $(this).attr("checked",status);
                    });
                };
Mi problema es que como veis uso un for each que los valores los quiero traer con ajax de otra fichero.
Lo que busco es simplemente al mandar el primer formulario, construir un segundo folmulario (el de class='select') con unos checkbox que tengan como valores los resultados del primer formulario.
Código PHP:
if(isset ($_POST['ajax']))
    {
        
$folderPath $_POST['folderPath'];
        
$fileExtensions $_POST['fileExtensions'];
        
        if(!
file_exists('libraries/fileExplorer.class.php')){
            exit (
'File does not exists');
        }
        
        require (
'libraries/fileExplorer.class.php');
        
$fileExplorer = new fileExplorer;
        
$files $fileExplorer->showFiles($folderPath$fileExtensions);        

    } 
  #2 (permalink)  
Antiguo 17/09/2011, 11:05
 
Fecha de Ingreso: junio-2009
Ubicación: Madrid
Mensajes: 402
Antigüedad: 14 años, 10 meses
Puntos: 20
Respuesta: Array desde php con Ajax

Bueno buscando y buscando decidi intentar traer todos los valores del array en un json y luego para cada uno crear la linea de codigo adecuada pero bueno, estoy alli con ello.

SI alguien tiene alguna idea al respeto, porfavor que comparta :D
  #3 (permalink)  
Antiguo 18/09/2011, 12:05
 
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.                 };

Etiquetas: ajax, php, post, select, formulario
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:53.