Ver Mensaje Individual
  #2 (permalink)  
Antiguo 28/06/2015, 13:48
Avatar de NSD
NSD
Colaborador
 
Fecha de Ingreso: mayo-2012
Ubicación: Somewhere
Mensajes: 1.332
Antigüedad: 12 años
Puntos: 320
Respuesta: ¿Evitar fin de petición asíncrona al hacer echo?

Pppfff este tema ya se trato tantas veces.

index.html
Código HTML:
Ver original
  1. <!DOCTYPE html>
  2.  <html>
  3.      <head>
  4.          <title>SELECT DEPENDIENTES</title>
  5.      </head>
  6.      <body>
  7.          <form id="form-depend">
  8.              <select name="continente" data-load="continente">
  9.                 <option value="" class="default">Seleccione</option>
  10.              </select>
  11.  
  12.              <select name="pais" data-load="pais" data-depend="continente">
  13.                 <option value="" class="default">Seleccione</option>
  14.              </select>
  15.  
  16.              <select name="provincia" data-load="provincia" data-depend="continente,pais">
  17.                 <option value="" class="default">Seleccione</option>
  18.              </select>
  19.          </form>
  20.  
  21.          <script>
  22.             function depend(form) {
  23.                 this.form = form;
  24.                 this.init();
  25.             }
  26.             depend.prototype = {
  27.                 "init" : function() {
  28.                     var dependents = this.form.querySelectorAll("select[data-depend]"),
  29.                         standalone = this.form.querySelectorAll("select:not([data-depend])"),
  30.                         dependent;
  31.  
  32.                     for (var d=0; d<dependents.length; d++) {
  33.                        dependent = dependents[d];
  34.                        var mains = this.form.querySelectorAll("[name='" + dependent.dataset.depend.split(",").join("'],[name='") +"']");
  35.                        for (var m=0; m<mains.length; m++)
  36.                            mains[m].addEventListener("change", this.load.bind(this, dependent));
  37.                    }
  38.                    
  39.                    for (var s=0; s<standalone.length; s++)
  40.                        this.load(standalone.item(s));  
  41.                },
  42.                "load" : function(select) {
  43.                    var options = select.querySelectorAll("option:not(.default)");
  44.                    for (var o=0; o<options.length; o++)
  45.                        select.removeChild(options.item(o));
  46.  
  47.                    this.ajax("combo/" + select.dataset.load + ".php", {
  48.                        "success" : function(data) {                            
  49.                            data.forEach(function(item){
  50.                                var option = document.createElement("option");
  51.                                option.value = item.value;
  52.                                option.label = item.label;
  53.                                select.appendChild(option);
  54.                            });
  55.                        },
  56.                        "error" : function(e) {
  57.                            console.log(e);
  58.                            alert(e);
  59.                        }
  60.                    });
  61.                },
  62.                "ajax" : function(url, callbacks) {
  63.                    var request = new XMLHttpRequest();
  64.                    request.open("post", url);
  65.                    request.onreadystatechange = function(callbacks) {
  66.                        if(this.readyState === 4) {
  67.                            var response = this.responseText,
  68.                                success = false;
  69.  
  70.                            if(this.status == 200) {
  71.                                try {
  72.                                    response = JSON.parse(response);
  73.                                    success = true;
  74.                                } catch(e) { }
  75.                            }
  76.  
  77.                            if(success)
  78.                                if(callbacks.success)
  79.                                    callbacks.success(response);
  80.                            else if(callbacks.error)
  81.                                callbacks.error({"response" : response});
  82.                        }
  83.                    }.bind(request, callbacks);
  84.                    request.send(new FormData(this.form));
  85.                }
  86.            }
  87.  
  88.            new depend(document.getElementById("form-depend"));
  89.         </script>
  90.      </body>
  91.  </html>

combo/continente.php
Código PHP:
Ver original
  1. <?php
  2.     $continentes = [
  3.         ["label" => "America", "value" => 1],
  4.         ["label" => "Europa", "value" => 2],
  5.         // ...
  6.     ];
  7.    
  8.     echo json_encode($continentes);

combo/pais.php
Código PHP:
Ver original
  1. <?php
  2.     $paises = [
  3.         1 => [
  4.             ["label" => "Argentina", "value" => 1],
  5.             ["label" => "Uruguay", "value" => 2]
  6.             // ...
  7.         ],
  8.         2 => [
  9.             ["label" => "España", "value" => 3],
  10.             ["label" => "Alemania", "value" => 4]
  11.             // ...
  12.         ],
  13.         // ...
  14.     ];
  15.    
  16.     echo json_encode(empty($paises[$_POST["continente"]]) ? [] : $paises[$_POST["continente"]]);

combo/provincia.php
Código PHP:
Ver original
  1. <?php
  2.     $provincias = [
  3.         1 => [
  4.             ["label" => "Buenos Aires", "value" => 1],
  5.             // ...
  6.         ],
  7.         2 => [
  8.             ["label" => "Montevideo", "value" => 3],
  9.             // ...
  10.         ],
  11.         3 => [
  12.             ["label" => "Madrid", "value" => 4],
  13.             // ...
  14.         ],
  15.         4 => [
  16.             ["label" => "Berlin", "value" => 5],
  17.             // ...
  18.         ],
  19.         // ...
  20.     ];
  21.    
  22.     echo json_encode(empty($provincias[$_POST["pais"]]) ? [] : $provincias[$_POST["pais"]]);
__________________
Maratón de desafíos PHP Junio - Agosto 2015 en FDW | Reglamento - Desafios