Ver Mensaje Individual
  #2 (permalink)  
Antiguo 01/01/2011, 17:09
Avatar de alfcm
alfcm
 
Fecha de Ingreso: mayo-2009
Mensajes: 291
Antigüedad: 15 años
Puntos: 7
Respuesta: ¿Como puedo hacer una lista enlazada con ajax?

Código HTML:
Ver original
  1. <script type="text/javascript">
  2. function nueAjax() {
  3.     try{
  4.         req = new XMLHttpRequest();
  5.     }catch(err1){
  6.         try{
  7.             req = new ActiveXObject("Msxml2.XMLHTTP");
  8.         }catch(err2){
  9.             try{
  10.                 req = new ActiveXObject("Microsoft.XMLHTTP");
  11.             }catch(err3){
  12.                 req = false;
  13.             }
  14.         }
  15.     }
  16.     return req;
  17. }
  18. var http = nueAjax();
  19.  
  20. function listamedico()
  21. {
  22.    
  23.      var capa=document.getElementById("lista");
  24.      http.open("POST","procmedico.php",true);
  25.      http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  26.     http.send("metodo=listar");
  27.     http.onreadystatechange=function(){
  28.         if (http.readyState==1){
  29.             capa.innerHTML="Procesando";
  30.         }
  31.         if (http.readyState==4){   
  32.             respuesta=http.responseText;
  33.             capa.innerHTML=respuesta;
  34.         }
  35.     }
  36. }
  37. </head>
  38. <h3>Mi listado</h3>
  39. <input type="button" value="Ver lista" onclick="listamedico()" />
  40. <br/>
  41. <div id="lista"></div>
  42. </body>
  43. </html>


tu clase Conectar.php

Código PHP:
Ver original
  1. <?php
  2. class Conectar{
  3.    
  4.     private $rs;
  5.         private $acmsistemas;
  6.         private $hostname_acmsistemas = "localhost";
  7.         private $database_acmsistemas = "acmsiste_alfamedic";
  8.         public $username_acmsistemas = "root";
  9.         public $password_acmsistemas = "";
  10.        
  11.     public function __construct(){
  12.      
  13.           $this->acmsistemas = mysql_pconnect($this->hostname_acmsistemas, $this->username_acmsistemas, $this->password_acmsistemas) or die("NO HAY CONEXION");
  14.        
  15.     }      
  16.        
  17.     public function consulta($sql)
  18.         {
  19.         if (!($this->acmsistemas === false))
  20.         {
  21.                 if (mysql_select_db($this->database_acmsistemas, $this->acmsistemas) === false)
  22.                 {
  23.                     echo('Error con la Base de Datos: ' . mysql_error());
  24.                     continue;
  25.                 }
  26.                 else
  27.                 {
  28.                 $this->rs=@mysql_query($sql);
  29.                     if(!$this->rs)
  30.                         {
  31.                                 echo 'No se puede ejecutar la consulta SQL';
  32.                         }
  33.                         else
  34.                         {
  35.                                 return $this->rs;              
  36.                         }
  37.                 }
  38.             }
  39.     }
  40.        
  41.     public function respuesta(){
  42.         return @mysql_fetch_object($this->rs);
  43.     }
  44.    
  45.     public function getRs(){
  46.         return $this->rs;      
  47.     }
  48. }
  49. ?>

tu archivo que hara los proceso de datos

procmedico.php
Código PHP:
Ver original
  1. include "Conectar.php";
  2.     $obj=new Conectar();
  3.    
  4.     $metodo=$_POST["metodo"];
  5.    
  6.     if($metodo == 'listar'){
  7.      
  8.        $sql="SELECT nombre,apellido,num_doc FROM medico";
  9.                
  10.                 $obj->consulta($sql);
  11.                 $i=1;
  12.                  
  13.                   $t="<table><tr><td>NOMBRE</td><td>APELLIDO</td><td>NUM_DOC</td></tr>";
  14.                  while($r=$obj->respuesta())
  15.                  {
  16.                      $t.="<tr><td>".$i."</td><td>".$r->nombre."</td><td>".$r->apellido."</td><td>".$r->num_doc."</td></tr>";
  17.                      $i=$i+1;
  18.                  }
  19.                  $t.="</table>";
  20.  
  21.                  echo $t;
  22.     }

y en tu base de datos creas tu tablita medico con campos nombre,apellido, num_doc


Saludos