Ver Mensaje Individual
  #2 (permalink)  
Antiguo 21/07/2010, 16:58
Avatar de Adler
Adler
Colaborador
 
Fecha de Ingreso: diciembre-2006
Mensajes: 4.671
Antigüedad: 17 años, 4 meses
Puntos: 126
Respuesta: Actualizar al datos

Hola

A ver si es esto lo que buscas
Código PHP:
Ver original
  1. <?php
  2. header('content-type: text/html; charset: utf-8');
  3. ?>
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  5. <html xmlns="http://www.w3.org/1999/xhtml"lang="es" xml:lang="es">
  6. <head>
  7. <meta name="http-equiv" content="Content-type: text/html; charset=UTF-8"/>
  8. <style type="text/css">
  9. .color0 {background: orange;}
  10. .color1 {background: yellow;}
  11. </style>
  12. <script type="text/javascript">
  13. var ns4 = (document.layers)? true:false
  14. var ie4 = (document.all)? true:false
  15. var ns6 = (document.getElementById)? true:false
  16.  
  17. function captura_objeto(idnombre) {
  18.     if (ns6) {
  19.     return document.getElementById(idnombre);
  20.     } else if (ie4) {
  21.     return document.all[idnombre];
  22.     } else if (ns4) {
  23.     return document.layers[idnombre];
  24.     } else{
  25.     return null;
  26.     }
  27. }
  28.  
  29.  
  30.  
  31. function creaAjax(){
  32.     var ajaxs = ["Msxml2.XMLHTTP","Msxml2.XMLHTTP.4.0","Msxml2.XMLH TTP.5.0","Msxml2.XMLHTTP.3.0","Microsoft.XMLHTTP"];
  33.     var ajax = false;
  34.     for(var i=0 ; !ajax && i<ajaxs.length ; i++){
  35.         try{
  36.             ajax = new ActiveXObject(ajaxs[i]);   // Internet Explorer
  37.         }
  38.         catch(e) {
  39.             ajax = false;
  40.         }
  41.     }
  42.     if(!ajax && typeof XMLHttpRequest!='undefined') {
  43.         ajax = new XMLHttpRequest();  // Firefox, Opera 8.0+, Safari
  44.     }
  45.     return ajax;
  46. }
  47.  
  48.  
  49.  
  50. var control = true;
  51. function ActivarDesactivar_campos(Id, bol, acc) {
  52. //alert(bol+ " - " +control);
  53. var Id_tr = captura_objeto(Id);
  54. var td = Id_tr.getElementsByTagName("TD");
  55. var div = Id_tr.getElementsByTagName("DIV");
  56.  
  57.     if(control == bol && bol == true && acc == 0) {
  58.  
  59.         div[1].style.display = (div[1].style.display == 'none') ? 'block' : 'none';
  60.         var elemento1 = document.createElement("INPUT");
  61.         elemento1.setAttribute("type","text");
  62.         elemento1.setAttribute("size","10");
  63.         elemento1.setAttribute("maxlength","20");
  64.         elemento1.setAttribute("name","nombre");
  65.         elemento1.setAttribute("id","nombre");
  66.         elemento1.setAttribute("value", div[1].innerHTML);
  67.         td[1].appendChild(elemento1);
  68.  
  69.         div[2].style.display = (div[2].style.display == 'none') ? 'block' : 'none';
  70.         var elemento2 = document.createElement("SELECT");
  71.         elemento2.setAttribute("size","1");
  72.         elemento2.setAttribute("name","privilegio");
  73.         elemento2.setAttribute("id","privilegio");
  74.         opcioncur = document.createElement("OPTION");
  75.         opcioncur.textContent = "Tipo de Permiso";
  76.         opcioncur.value = div[2].innerHTML;
  77.         elemento2.appendChild(opcioncur);
  78.         opcion1 = document.createElement("OPTION");
  79.         opcion1.textContent = "Alto";
  80.         opcion1.value = "A";
  81.         elemento2.appendChild(opcion1);
  82.         opcion2 = document.createElement("OPTION");
  83.         opcion2.textContent = "Medio";
  84.         opcion2.value = "D";
  85.         elemento2.appendChild(opcion2);
  86.         opcion3 = document.createElement("OPTION");
  87.         opcion3.textContent = "Bajo";
  88.         opcion3.value = "F";
  89.         elemento2.appendChild(opcion3);
  90.         td[2].appendChild(elemento2);
  91.  
  92. control = (bol == true ? false : true);
  93.  
  94.     } else if(control == bol && bol == false && acc == 0) {
  95.  
  96.         var elem1 = document.getElementById("nombre");
  97.         var padre1 = elem1.parentNode;
  98.         padre1.removeChild(elem1);
  99.         div[1].style.display = (div[1].style.display == 'none') ? 'block' : 'none';
  100.  
  101.         var elem2 = document.getElementById("privilegio");
  102.         var padre2 = elem2.parentNode;
  103.         padre2.removeChild(elem2);
  104.         div[2].style.display = (div[2].style.display == 'none') ? 'block' : 'none';
  105.  
  106. control = (bol == true ? false : true);
  107.        
  108.     } else if(control == bol && bol == false && acc == 1) {
  109.  
  110.  
  111. // --- REVISAMOS LOS VALORES (sin son iguales no se usa Ajax)
  112. var elem1_old = div[1].innerHTML
  113. var elem1 = document.getElementById("nombre");
  114. var elem2_old = div[2].innerHTML
  115. var elem2 = document.getElementById("privilegio");
  116.  
  117.         if (elem1_old == elem1.value && elem2_old == elem2.value) {
  118.             div[1].textContent = elem1_old;
  119.             div[2].textContent = elem2_old;
  120.  
  121.         // ---- ELIMINA LOS ELEMENTOS
  122.             var padre1 = elem1.parentNode;
  123.             padre1.removeChild(elem1);
  124.             var padre2 = elem2.parentNode;
  125.             padre2.removeChild(elem2);
  126.  
  127.         // ---- MUESTRA REGISTRO SIN ACTUALIZAR
  128.             div[1].style.display = (div[1].style.display == 'none') ? 'block' : 'none';
  129.             div[2].style.display = (div[2].style.display == 'none') ? 'block' : 'none';
  130. control = true;
  131.  
  132.         } else {
  133.  
  134. // ---- ACTUALIZA REGISTRO
  135. var url = "Otorgar_Permisos_Ajax2.php"
  136. var ajax=creaAjax();
  137. //var elem1 = document.getElementById("nombre");
  138. //var elem2 = document.getElementById("privilegio");
  139. ajax.open('POST',url,true);
  140. ajax.onreadystatechange = function() {
  141.  
  142.             if (ajax==null){
  143.                 alert ("Tu navegador web no soporta AJAX!");
  144.                 return;
  145.             }
  146.  
  147.             if (ajax.readyState==1 || ajax.readyState==2 || ajax.readyState==3){
  148.  
  149.                     } else if (ajax.readyState==4) {
  150.                             if (ajax.status==200) {        
  151.                     var valores = unescape(ajax.responseText); 
  152.                     div[1].textContent = valores.split(",")[0];
  153.                     div[2].textContent = valores.split(",")[1];
  154.  
  155.             // ---- ELIMINA LOS ELEMENTOS
  156.                     var padre1 = elem1.parentNode;
  157.                     padre1.removeChild(elem1);
  158.                     var padre2 = elem2.parentNode;
  159.                     padre2.removeChild(elem2);
  160.  
  161.             // ---- MUESTRA REGISTRO ACTUALIZADO
  162.                     div[1].style.display = (div[1].style.display == 'none') ? 'block' : 'none';
  163.                     div[2].style.display = (div[2].style.display == 'none') ? 'block' : 'none';
  164.  
  165.                             } else if (ajax.status==404) {
  166.                                             alert("La dirección no existe");
  167.  
  168.                                                     } else {
  169.                                         alert("Se ha producido un error");
  170.                                                     }
  171.                                         }
  172.                     }
  173. control = true;
  174.    
  175.     ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  176.             ajax.setRequestHeader('Content-Length', Id_tr.length); 
  177.     ajax.send('id='+Id+'&nombre='+elem1.value+'&nombre_old='+div[1].innerHTML+'&privilegio='+escape(elem2.value)+'&privilegio_old='+escape(div[2].innerHTML));
  178.             return;
  179.         }
  180.     }
  181. }
  182. </script>
  183. </head>
  184. <body>
  185. <table width="100%" border="1px">
  186. <tr>
  187. <th></th>
  188. <th>ID</th>
  189. <th>NOMBRE</th>
  190. <th>PRIVILEGIOS</th>
  191. </tr>
  192.  
  193. <?php
  194. include($_SERVER["DOCUMENT_ROOT"]."/ink_utiles/ink_comunesbd.inc.php");  
  195. $bd = new ClassConexionBD('database');
  196. $bd->ConexionBD();
  197.  
  198. $sql = $bd->consulta("SELECT Id, nombre, privilegio FROM Usuarios");
  199.  
  200.     if($bd->RS($sql)>0) {  
  201. $tabla = 0;
  202.         while($registros = $bd->arreglo($sql)){  
  203. $resto = ($tabla % 2);
  204.             switch ($resto) { // Coloreamos cada tr        
  205.                 case 0: $estilo = "color0"; break;        
  206.                 case 1: $estilo = "color1"; break;
  207.             }
  208.  
  209. //------------------------- Valores de la consulta -------------------------
  210. ?>
  211. <tr width="100%" id="<?=$registros["Id"]?>">
  212. <th><img src="./cancelar.gif" onclick="javascript:ActivarDesactivar_campos(<?=$registros["Id"]?>, false, 0);" />&nbsp;<img src="./editar.gif" onclick="javascript:ActivarDesactivar_campos(<?=$registros["Id"]?>, false, 1);" />&nbsp;<img src="./mostar.gif" onclick="javascript:ActivarDesactivar_campos(<?=$registros["Id"]?>, true, 0);" /></th>
  213. <td id="td_<?=$tabla?>[]" class="<?=$estilo?>">
  214. <div id="<?=$tabla?>[]" style="display:block;"><?=$registros["Id"]?></div>
  215. </td>
  216. <td id="td_<?=$tabla?>[]" class="<?=$estilo?>">
  217. <div id="<?=$tabla?>[]" style="display:block;"><?=$registros["nombre"]?></div>
  218. </td>
  219. <td id="td_<?=$tabla?>[]" class="<?=$estilo?>">
  220. <div id="<?=$tabla?>[]" style="display:block;"><?=$registros["privilegio"]?></div>
  221. </td>
  222. </tr>
  223. <?php
  224. $tabla++;
  225. //------------------------------------------------------
  226.         }
  227.     }
  228. $bd->limparRS($sql);
  229. $bd->limpiarCon();
  230. ?>
  231. </table>
  232. </body>
  233. </html>

otorgar_permisos_ajax2.php
Código PHP:
Ver original
  1. <?php
  2. header('content-type: text/html; charset: utf-8');
  3. $campos_valor_actualizar = Array();
  4. $respuesta = "";
  5.  
  6. $nomb = trim($_POST["nombre"]);
  7. $nomb_old = trim($_POST["nombre_old"]);
  8. $priv = trim($_POST["privilegio"]);
  9. $priv_old = trim($_POST["privilegio_old"]);
  10.  
  11. if ($nomb == $nomb_old) {
  12. $respuesta = $nomb_old;
  13. } else {
  14. $respuesta = $nomb;
  15. array_push($campos_valor_actualizar, "nombre = '".$nomb."'");
  16. }
  17.  
  18. if ($priv == $priv_old) {
  19. $respuesta .= "," .$priv_old;
  20. } else {
  21. $respuesta .= "," .$priv;
  22. array_push($campos_valor_actualizar, "privilegio = '".$priv."'");
  23. }
  24.  
  25. include($_SERVER["DOCUMENT_ROOT"]."/ink_utiles/ink_comunesbd.inc.php");  
  26. $bd = new ClassConexionBD('database');
  27. $bd->ConexionBD();
  28.  
  29. $sql = $bd->consulta("UPDATE Usuarios SET " .implode(",", $campos_valor_actualizar). " WHERE Id = ".$_POST["id"]."");
  30. //$bd->limparRS($sql);
  31. $bd->limpiarCon();
  32.  
  33.  
  34. echo $respuesta;
  35. ?>
Suerte
__________________
Los formularios se envían/validan con un botón Submit
<input type="submit" value="Enviar" style="background-color:#0B5795; font:bold 10px verdana; color:#FFF;" />