Ver Mensaje Individual
  #12 (permalink)  
Antiguo 09/07/2015, 09: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: Autorellenar formulario después de consulta

carga.html
Código HTML:
Ver original
  1. <!DOCTYPE html>
  2.     <head>
  3.         <meta charset="utf-8">
  4.         <title>Carga de datos</title>
  5.         <script>
  6.             LoadData = function(url) {
  7.                 this.search = this.search.bind(this);
  8.                 this.load = this.load.bind(this);
  9.  
  10.                 this.url = url;
  11.                 this.form = null;
  12.             }
  13.  
  14.             LoadData.prototype = {
  15.                 "search" : function(form) {
  16.                     this.form = form;
  17.                     this.load(new FormData(this.form), function(status, response) {
  18.                         if(status === 200) {
  19.                             response = JSON.parse(response);                           
  20.                             for(var name in response)
  21.                                 this.form.elements[name].value = response[name];
  22.                         }
  23.                     });
  24.                 },
  25.                 "load" : function(params, callback) {
  26.                     var request = new XMLHttpRequest();
  27.                     request.open("POST", this.url);
  28.                     request.onreadystatechange = function(callback) {
  29.                         if(this.readyState == 4)
  30.                             callback(this.status, this.responseText);
  31.                     }.bind(request, callback.bind(this));
  32.                     request.send(params);
  33.                 }
  34.             };
  35.         </script>
  36.     </head>
  37.     <body>
  38.         <h1>Carga de formulario de busqueda via json</h1>
  39.         <form id="datos-origen">
  40.             <label>
  41.                 El codigo:
  42.                 <input type="text" name="codigo" value="5">
  43.             </label>
  44.             <label>
  45.                 Nombres:
  46.                 <input type="text" name="nombres">
  47.             </label>
  48.             <label>
  49.                 Apellidos:
  50.                 <input type="text" name="apellidos">
  51.             </label>
  52.             <label>
  53.                 Documento:
  54.                 <input type="text" name="documento_nro">
  55.             </label>
  56.             <input type="reset" value="VACIAR">
  57.         </form>
  58.         <button id="load">Cargar</button>
  59.  
  60.         <script>
  61.             document.getElementById("load").addEventListener("click", function() {
  62.                 (new LoadData("search.php")).search(document.getElementById("datos-origen"));
  63.             });
  64.         </script>
  65.     </body>
  66. </html>

search.php
Código PHP:
Ver original
  1. <?php    
  2. /* Config */
  3. $db = ["localhost", "root", "", "db_name"];
  4. $table = "contactos";
  5. $fields = [
  6.     "codigo" => "=",
  7.     "nombres" => "%LIKE%",
  8.     "apellidos" => "%LIKE%",
  9.     "documento_nro" => null
  10. ];
  11. $result = false;
  12.  
  13. if($mysqli = new MySQLi($db[0], $db[1], $db[2], $db[3])) {
  14.  
  15.     $filter = array_intersect_key(array_filter($fields), array_filter($_POST));
  16.  
  17.     $where = "";
  18.     foreach($filter as $key => $type) {
  19.         $where .= ($where ? " AND " : "");
  20.         switch($type) {
  21.             case "=" :
  22.                 $where .= "$key = ?";
  23.             break;
  24.             case "%LIKE%" :
  25.                 $where .= "$key LIKE ?";
  26.                 $_POST[$key] = "%" . $_POST[$key] . "%";
  27.             break;
  28.         }
  29.     }
  30.    
  31.     if($where)
  32.         $where = "WHERE $where";
  33.    
  34.     if($stmt = $mysqli->prepare("SELECT " . implode(", ", array_keys($fields)). " FROM $table $where LIMIT 1")) {
  35.        
  36.         foreach($filter as $key => $type)
  37.             $stmt->bind_param("s", $_POST[$key]);
  38.  
  39.         $stmt->execute();
  40.  
  41.         if($result = $stmt->get_result()) {
  42.             $result = $result->fetch_array(MYSQLI_ASSOC);
  43.             foreach($result as &$value)
  44.                 $value = utf8_encode($value);
  45.             unset($value);
  46.         }  
  47.         $stmt->close();  
  48.     }
  49.    
  50.     $mysqli->close();  
  51. }
  52.  
  53. if(!$result)
  54.     $result = array_fill_keys(array_keys($fields), null);
  55.  
  56. exit(json_encode($result));
__________________
Maratón de desafíos PHP Junio - Agosto 2015 en FDW | Reglamento - Desafios